Example #1
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');
 }
Example #2
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;
 }