Router

Standard router

Based on the PhalconPHP router. Global router rules are placed in /app/config/routes.php directory, but local module rules are in /app/modules/{Foo}/config/routes.php where {Foo} is a module name.

return [
    'home' =>  [ //Route name
        'route' => '/', //Route url
        'paths' => [
            'module' => 'Home',
            'controller' => 'Frontend\Home',
            'action' =>  'index'
        ]
    ]
];

Route types

Available types:

  • Default
    return [
        'home' =>  [
            'route' => '/',
            'paths' => [
                'module' => 'Home',
                'controller' => 'Frontend\Home',
                'action' =>  'index'
            ],
            'type' => 'default' //Optional
        ]
    ];

  • Static - works the same way like default route, but cannot be overwritten by another rules.
    return [
        'login' =>  [
            'route' => '/login',
            'paths' => [
                'module' => 'Auth',
                'controller' => 'Frontend\Auth',
                'action' => 'login'
            ],
            'type' => 'static'
        ]
    ];

  • Base - works the same way like default route, but will be overwritten by any other (default or static) rule.
    return [
        'page' =>  [
            'route' => '/{slug}',
            'paths' => [
                'module' => 'Page',
                'controller' => 'Frontend\Page',
                'action' =>  'index'
            ],
            'type' => 'base'
        ]
    ];

  • Rest - using to create REST Api. Allows to map HTTP methods with controller actions.
    return [
        'products' =>  [
            'route' => '/products',
            'paths' => [
                'module' => 'Product',
                'controller' => 'Frontend\Product'
            ],
            'type' => 'rest',
            'params' => [
                'actions' => [
                    '/' =>  [
                        'index' => Vegas\Http\Method::GET,
                        'create' => Vegas\Http\Method::POST
                    ],
                    '/{id}' =>  [
                        'show' => Vegas\Http\Method::GET,
                        'update' => Vegas\Http\Method::PUT,
                        'delete' => Vegas\Http\Method::DELETE
                    ]
                ]
            ]
        ]
    ];
    

  • Not found - is used when no route is matched
    return [
        'not-found' => [
            'route' => '',
            'paths' => [
                'module' => 'Home',
                'controller' => 'Frontend\Error',
                'action' => 'error404'
            ],
            'type' => 'notfound'
        ]
    ];

Hostname constraints

Adding the parameter hostname we can constrain matching the route only when $_SERVER['HTTP_HOST'] is equals to defined hostname. Remember to define the base hostname in application config, for example:

in application config:


    ...
    'hostname' => 'example.com'
    ...
    

route definition:


return [
    'not-found' => [
        'route' => '/foo',
        'paths' => [
            'module' => 'Foo',
            'controller' => 'Frontend\Foo',
            'action' => 'index'
        ],
        'type' => 'default',
        'params' => [
            'hostname' => 'foo.example.com'
        ]
    ]
];

Own router

The standard router can be replaced with your own custom router. You can do it by overriding method initRoutes inside application bootstrap

Base routes initialization looks like:


protected function initRoutes()
{
    //setups router
    $routerAdapter = new \Vegas\Mvc\Router\Adapter\Standard($this->di);
    $router = new \Vegas\Mvc\Router($this->di, $routerAdapter);

    //adds routes defined in modules
    $modules = $this->application->getModules();
    foreach ($modules as $module) {
        $router->addModuleRoutes($module);
    }

    //adds routes defined in default file
    $defaultRoutesFile = $this->config->application->configDir . DIRECTORY_SEPARATOR . 'routes.php';
    if (file_exists($defaultRoutesFile)) {
        $router->addRoutes(require $defaultRoutesFile);
    }

    //setup router rules
    $router->setup();

    $this->di->set('router', $router->getRouter());
}

Create your own adapter, that implements interface Phalcon\Mvc\RouterInterface and pass it as second argument to the constructor of class \Vegas\Mvc\Router.