public function testConstructor() { $parsedRoute = new ParsedRoute("Vendor\\Package\\{controller}::action", ["param1" => 1, "param2" => "two"]); $this->assertSame("Vendor\\Package\\{controller}::action", $parsedRoute->controller()); $this->assertSame(1, $parsedRoute->params()["param1"]); $this->assertSame("two", $parsedRoute->params()["param2"]); }
/** * @param ParsedRoute $route * @return string * * @throws RuntimeException */ public function handle(ParsedRoute $route) { $segments = explode("::", $route->controller()); $controller = $segments[0]; $action = count($segments) > 1 ? $segments[1] : "index"; if (method_exists($controller, $action)) { $params = [$route->params()]; return call_user_func_array([new $controller(), $action], $params); } else { throw new RuntimeException("No method {$action} in controller {$segments[0]}"); } }
/** * @param ParsedRoute $route * @return string * * @throws RuntimeException */ public function handle(ParsedRoute $route) { $controller = $route->controller(); $rawParams = $route->params(); foreach ($rawParams as $param => $value) { if (strpos($controller, "{" . $param . "}") !== false) { $controller = str_replace("{" . $param . "}", ucfirst($value), $controller); unset($rawParams[$param]); } } $segments = explode("::", $controller); $controller = $segments[0]; $action = count($segments) > 1 ? $segments[1] : "index"; if (method_exists($controller, $action)) { $params = [$rawParams]; return call_user_func_array([new $controller(), $action], $params); } else { throw new RuntimeException("No method {$action} in controller {$segments[0]}"); } }