Exemplo n.º 1
0
 /**
  * Adds a route applying the common attributes
  */
 protected function _addRoute($pattern, $paths = null, $httpMethods = null)
 {
     /**
      * Check if the paths need to be merged with current paths
      */
     $defaultPaths = $this->_paths;
     if (is_array($defaultPaths)) {
         if (is_string($paths)) {
             $processedPaths = Route::getRoutePaths($paths);
         } else {
             $processedPaths = $paths;
         }
         if (is_array($processedPaths)) {
             /**
              * Merge the paths with the default paths
              */
             $mergedPaths = array_merge($defaultPaths, $processedPaths);
         } else {
             $mergedPaths = $defaultPaths;
         }
     } else {
         $mergedPaths = $paths;
     }
     /**
      * Every route is internally stored as a Phalcon\Mvc\Router\Route
      */
     $route = new Route($this->_prefix . $pattern, $mergedPaths, $httpMethods);
     $route->setDI($this->getDI());
     $this->_routes[] = $route;
     $route->setGroup($this);
     return $route;
 }
Exemplo n.º 2
0
 /**
  * @param string $pattern
  * @param null $paths
  * @param null $httpMethods
  * @param int|mixed $position
  * @return Route
  * @throws Exception
  */
 public function add($pattern, $paths = null, $httpMethods = null, $position = parent::POSITION_LAST)
 {
     /**
      * Every route is internally stored as a Phalcon\Mvc\Router\Route
      */
     $route = new Route($pattern, $paths, $httpMethods);
     $route->setDI($this->getDI());
     switch ($position) {
         case parent::POSITION_LAST:
             $this->_routes[] = $route;
             break;
         case parent::POSITION_FIRST:
             $this->_routes = array_merge([$route], $this->_routes);
             break;
         default:
             throw new Exception("Invalid route position");
     }
     return $route;
 }
Exemplo n.º 3
0
 /**
  * {@inheritdoc}
  */
 public function add(RouterInterface $router, Route $route)
 {
     //resolves actions with http methods
     $actions = $route->getParam('actions');
     if (empty($actions)) {
         $actions = array('/' => array('index' => Method::GET));
     }
     //add routes with http method
     //for each action new route is adding with specified HTTP Method.
     foreach ($actions as $actionRoute => $actionMethods) {
         if ($actionRoute == '/') {
             $actionRoute = '';
         }
         foreach ($actionMethods as $action => $method) {
             $paths = $route->getPaths();
             $paths['action'] = $action;
             $newRoute = $router->add($route->getRoute() . $actionRoute, $paths)->via($method);
             $newRoute->setName($route->getName() . $actionRoute . '/' . $action);
             $newRoute->setHostName($route->getParam('hostname'));
         }
     }
 }
Exemplo n.º 4
0
 /**
  * Setups router
  * Adds rules to router
  */
 public function setup()
 {
     $this->groupRoutes();
     foreach ($this->routes as $type => $routes) {
         foreach ($routes as $name => $route) {
             $newRoute = new Route($name, $route);
             if (!$newRoute->hasParam('hostname')) {
                 $newRoute->setParam('hostname', $this->resolveDefaultHostName());
             }
             $routeType = $this->resolveRouteType($type);
             $routeType->add($this->getRouter(), $newRoute);
         }
     }
 }
Exemplo n.º 5
0
 /**
  * {@inheritdoc}
  */
 public function add(RouterInterface $router, Route $route)
 {
     $router->notFound($route->getPaths());
 }
Exemplo n.º 6
0
 /**
  * {@inheritdoc}
  */
 public function add(RouterInterface $router, Route $route)
 {
     $router->add($route->getRoute(), $route->getPaths())->setName($route->getName())->setHostName($route->getParam('hostname'));
 }
Exemplo n.º 7
0
 /**
  * {@inheritdoc}
  */
 public function add(RouterInterface $router, Route $route)
 {
     $router->setDefaults(array_merge($route->getPaths(), ['params' => $route->getParams()]));
 }
Exemplo n.º 8
0
 public function testShouldMapConstructorParametersToClassProperties()
 {
     $routeArray = ['route' => '/url', 'paths' => array('module' => 'module', 'controller' => 'controller', 'action' => 'action', 'auth' => array('auth', 'authAdmin')), 'type' => 'static', 'params' => array('param_1' => 'value_1', 'param_2' => 'value_2', 'param_3' => 'value_3')];
     $route = new Route('test', $routeArray);
     $this->assertEquals($route->getName(), 'test');
     $this->assertArrayHasKey('module', $route->getPaths());
     $this->assertArrayHasKey('controller', $route->getPaths());
     $this->assertArrayHasKey('action', $route->getPaths());
     $this->assertArrayHasKey('auth', $route->getPaths());
     $this->assertEquals('value_1', $route->getParam('param_1'));
     $this->assertEquals('value_2', $route->getParam('param_2'));
     $this->assertEquals('value_3', $route->getParam('param_3'));
     $this->assertSame(json_encode(['auth', 'authAdmin']), $route->getPaths()['auth']);
     $this->assertEquals('/url', $route->getRoute());
     $this->assertInternalType('array', $route->getParams());
     $this->assertInternalType('string', $route->getRoute());
     $this->assertInternalType('array', $route->getPaths());
     $this->assertInternalType('string', $route->getName());
 }