Skip to content

Commit

Permalink
RouterTrait treat and process
Browse files Browse the repository at this point in the history
  • Loading branch information
robsonvleite committed Sep 30, 2019
1 parent 3110e07 commit c9f26cf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 20 deletions.
3 changes: 2 additions & 1 deletion exemple/controller/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@

$router->get("/", "Name:home", "name.home");
$router->get("/hello", "Name:hello", "name.hello");
$router->get("/redirect", "Name:redirect", "name.redirect");

$router->get("/redirect", "Name:redirect", "name.redirect");
$router->get("/redirect/{category}/{page}", "Name:redirect", "name.redirect");

$router->get("/params/{category}/page/{page}", "Name:params", "name.params");

/**
Expand Down
35 changes: 16 additions & 19 deletions src/RouterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ public function route(string $name, array $data = null): ?string
{
foreach ($this->routes as $http_verb) {
foreach ($http_verb as $route_item) {
return $this->treat($name, $route_item, $data);
if (!empty($route_item["name"]) && $route_item["name"] == $name) {
return $this->treat($route_item, $data);
}
}
}
return null;
Expand Down Expand Up @@ -54,9 +56,8 @@ public function redirect(string $route, array $data = null): void
* @param string $route
* @param string|callable $handler
* @param null|string
* @return Dispatch
*/
protected function addRoute(string $method, string $route, $handler, string $name = null): RouterTrait
protected function addRoute(string $method, string $route, $handler, string $name = null): void
{
if ($route == "/") {
$this->addRoute($method, "", $handler, $name);
Expand Down Expand Up @@ -87,7 +88,6 @@ protected function addRoute(string $method, string $route, $handler, string $nam

$route = preg_replace('~{([^}]*)}~', "([^/]+)", $route);
$this->routes[$method][$route] = $router();
return $this;
}

/**
Expand All @@ -110,29 +110,26 @@ private function action($handler): ?string
}

/**
* @param string $name
* @param array $route_item
* @param array|null $data
* @return string|null
*/
private function treat(string $name, array $route_item, array $data = null): ?string
private function treat(array $route_item, array $data = null): ?string
{
if (!empty($route_item["name"]) && $route_item["name"] == $name) {
$route = $route_item["route"];
if (!empty($data)) {
$arguments = [];
$params = [];
foreach ($data as $key => $value) {
if (!strstr($route, "{{$key}}")) {
$params[$key] = $value;
}
$arguments["{{$key}}"] = $value;
$route = $route_item["route"];
if (!empty($data)) {
$arguments = [];
$params = [];
foreach ($data as $key => $value) {
if (!strstr($route, "{{$key}}")) {
$params[$key] = $value;
}
$route = $this->process($route, $arguments, $params);
$arguments["{{$key}}"] = $value;
}
return "{$this->projectUrl}{$route}";
$route = $this->process($route, $arguments, $params);
}
return null;

return "{$this->projectUrl}{$route}";
}

/**
Expand Down

0 comments on commit c9f26cf

Please sign in to comment.