예제 #1
0
파일: Route.php 프로젝트: corviz/framework
 /**
  * Build a new route and add it to the Map.
  *
  * @param array  $methods  Array containing http methods.
  *                         The supported methods are defined by Request::METHOD_* constants
  * @param string $routeStr A string that may contain parameters
  *                         that will be passed to the controller.
  *                         For example:
  *                         - /home
  *                         - /product/{productId}
  *                         - /tag/{slug}
  * @param array  $info     An array containing the following indexes:
  *                         - controller: Name of a controller class
  *                         - action: Method of the defined controller (Default: index)
  *                         - alias: Short name of the route, for easy referencing
  */
 public static function create(array $methods, string $routeStr, array $info)
 {
     $middlewareList = isset($info['middleware']) ? (array) $info['middleware'] : [];
     array_walk_recursive(self::$middlewareGroupStack, function ($middleware) use(&$middlewareList) {
         $middlewareList[] = $middleware;
     });
     $route = new self();
     $route->setMethods($methods);
     $route->setAction(isset($info['action']) ? $info['action'] : 'index');
     $route->setAlias(isset($info['alias']) ? $info['alias'] : '');
     $route->setMiddlewareList($middlewareList);
     $route->setControllerName($info['controller']);
     $route->setMethods($methods);
     $route->setRouteStr($routeStr);
     Map::addRoute($route);
 }
예제 #2
0
 public function testGetCurrentRouteShouldSelectCorrectInformationForAmbiguousPaths()
 {
     //setup
     $this->clearMap();
     Route::all('posts/edit/{id}', ['controller' => 'Test', 'alias' => 'route1']);
     Route::all('{somePage}', ['controller' => 'Test', 'alias' => 'route2']);
     Route::all('posts/{id}', ['controller' => 'Test', 'alias' => 'route3']);
     //Test 1
     $this->setCurrentRequest(Request::METHOD_GET, '/posts/1/');
     $data = Map::getCurrentRoute();
     $this->assertEquals('route3', $data['alias'], 'incorrect route selected in test case 1');
     //Test 2
     $this->setCurrentRequest(Request::METHOD_GET, '/posts/edit/1/');
     $data = Map::getCurrentRoute();
     $this->assertEquals('route1', $data['alias'], 'incorrect route selected in test case 2');
     //Test 3
     $this->setCurrentRequest(Request::METHOD_GET, '/home/');
     $data = Map::getCurrentRoute();
     $this->assertEquals('route2', $data['alias'], 'incorrect route selected in test case 3');
 }
예제 #3
0
 /**
  * {@inheritdoc}
  */
 public function run(...$args)
 {
     //TODO: turn code into parts (methods)
     self::$current = $this;
     //Load application definitions.
     $this->loadRoutes();
     $this->registerRequestParsers();
     $this->registerProviders();
     //Call controller action.
     $this->request = Request::current();
     $route = Map::getCurrentRoute();
     if ($route) {
         $routeStr = ParametrizedString::make($route['route']);
         $params = array_replace($routeStr->getValues($this->request->getRouteStr()), $this->request->getQueryParams());
         $controller = $this->container->get($route['controller']);
         //Check controller
         if (!$controller instanceof Controller) {
             throw new \Exception("Invalid controller: {$route['controller']}");
         }
         /*
          * Call controller and process midware list
          */
         $middlewareList = $this->buildMiddlewareQueue([$route['middlewareList'], $controller->getMiddlewareList()]);
         $application = $this;
         $fn = function () use($controller, &$params, &$route, $application) {
             $response = $application->getContainer()->invoke($controller, $route['action'], $params);
             return ResponseFactory::createResponse($response);
         };
         $response = $this->proccessMiddlewareQueue($middlewareList, $fn);
         $response->send();
     }
     self::$current = null;
 }