pages/ -> matches /pages/ pages/$id -> matches /pages/1 ... (id => 1) pages/slug/$slug -> matches /pages/slug/some-slug-here (slug => some-slug-here) For the purposes of this class a URI path is broken into parts delimited with a '/'. There are two kinds of path parts: static and parametric. Static matches have precedence over parametric matches. For example, if you have the following routes: (1) /pages/$page_title/ (2) /pages/a-page/ (3) /pages/$page_title/$id A request of "/pages/a-page/" will match (2) and the result will not contain an argument. A request of "/pages/b-page/" will match (1) and the result will contain argument ("page_title" => "b_page") A request of "/pages/a-page/1" will match (3) with result arguments ("page_title" => "a_page", "id" => "1") Note: Because routing trees are serialized and unserialized frequently I am breaking the naming conventions and using short, one-letter member names.
 public function getControllerFor(Request $request, RtNode $routes)
 {
     $routeResult = $routes->findRouteFor($request);
     if ($routeResult->routeExists) {
         if ($routeResult->methodIsSupported) {
             $controller = $this->getControllerFromRouteResult($request, $routeResult);
         } else {
             throw new RecessResponseException('METHOD not supported, supported METHODs are: ' . implode(',', $routeResult->acceptableMethods), ResponseCodes::HTTP_METHOD_NOT_ALLOWED, get_defined_vars());
         }
     } else {
         throw new RecessResponseException('Resource does not exist.', ResponseCodes::HTTP_NOT_FOUND, get_defined_vars());
     }
     Application::activate($request->meta->app);
     $this->controller = $controller;
     return $controller;
 }
Exemple #2
0
 function addRoutesToRouter(RtNode $router)
 {
     $classes = $this->listControllers();
     foreach ($classes as $class) {
         if (Library::classExists($this->controllersPrefix . $class)) {
             $instance = new $class($this);
         } else {
             continue;
         }
         if ($instance instanceof Controller) {
             $routes = Controller::getRoutes($instance);
             if (!is_array($routes)) {
                 continue;
             }
             foreach ($routes as $route) {
                 $router->addRoute($this, $route, $this->routingPrefix);
             }
         }
     }
     return $router;
 }