function route($name) { $routes = RouteCollection::instance(); if (isset($routes[$name])) { $item = $routes[$name]->path; if (func_num_args() == 2) { $params = func_get_arg(1); $keys = array_keys($params); foreach ($keys as $key) { $item = str_replace("{" . $key . "}", $params[$key], $item); } } return $item; } throw new ResourceNotFoundException("No route named {$name} found. " . $routes->count()); }
private static function registerRoute($type, $uri, $arg2) { $controller = null; $closure = null; $name = md5(microtime()); if (is_array($arg2)) { if (isset($arg2['uses'])) { $controller = $arg2['uses']; } if (isset($arg2['as'])) { $name = $arg2['as']; } } else { if (is_string($arg2)) { $controller = $arg2; } else { if (gettype($arg2) == 'object' && $arg2 instanceof \Closure) { $closure = $arg2; } } } if ($controller == null && !$arg2 instanceof \Closure) { throw new IllegalArgumentException("Unable to set controller for route {$uri}."); } if (!strstr($controller, "@") && !$arg2 instanceof \Closure) { throw new IllegalArgumentException("Unable to set controller for route {$uri}: no controller method set."); } $instance = new self(); $instance->conditions = array(); $instance->name = $name; $instance->path = $uri; $instance->method = strtoupper($type); $instance->controller = $controller; $instance->closure = $closure; $collection = RouteCollection::instance(); $collection->add($name, $instance); return $instance; }
public function handleRequest() { ob_start(); $this->startSession(); $routes = RouteCollection::instance(); if ($routes == null || $routes->count() == 0) { throw new \ErrorException('Unable to load routes from configuration directory.'); } $request = Request::createFromGlobals(); $path = $request->getUri()->getPath(); $current_route = null; $matches = array(); $urlMatcher = new UrlMatcher($routes, $request); switch ($urlMatcher->matches($path)) { case UrlMatcher::UNKNOWN_RESOURCE: throw new ResourceNotFoundException('Route not found: ' . $path); return; case UrlMatcher::METHOD_MISMATCH: throw new MethodNotAllowedException("Method not allowed"); return; case UrlMatcher::MATCH_FOUND: $current_route = $urlMatcher->getMatchedRoute(); $matches = $urlMatcher->getMatches(); break; } // we use a controller, so do checks and throw if something is wrong (class or method) if (!$current_route->controller && !$current_route->closure) { throw new IllegalArgumentException('No controller or Closure was set for route ' . $current_route->path); } $params = array(); $userObject = null; // if we use a closure, call them with the request object if ($current_route->closure) { $userObject = $current_route->closure; $ref = new \ReflectionFunction($current_route->closure); if ($ref->getNumberOfParameters() > 0) { $first = $ref->getParameters()[0]; if ($first->getClass() != null && strstr($first->getClass()->getName(), 'Request')) { $params[] = $request; } foreach ($matches as $match) { $params[] = $match; } } } else { // handle controller $parts = explode("@", $current_route->controller); $controller = "App\\Controllers\\" . $parts[0]; $method = $parts[1]; if (!class_exists($controller)) { throw new ResourceNotFoundException("Unable to find controller {$controller}."); } if (!method_exists($controller, $method)) { throw new ResourceNotFoundException("Unable to call {$controller}::{$method}()"); } $instance = new $controller(); $userObject = array($instance, $method); $ref = new \ReflectionClass($controller); $num_params = $ref->getMethod($method)->getNumberOfParameters(); $retval = null; if ($num_params > 0) { $ref_method = $ref->getMethod($method); $first = $ref_method->getParameters()[0]; if ($first->getClass() != null && strstr($first->getClass()->getName(), 'Request')) { $params[] = $request; } foreach ($matches as $match) { $params[] = $match; } } if ($instance instanceof BaseController) { $instance->onBefore(); } } $retval = call_user_func_array($userObject, $params); $this->handleContent($retval); }