Router

Standard router

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

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

Route types

Available types:

  • Default
    return array(
        'home' =>  array(
            'route' => '/',
            'paths' => array(
                '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 array(
        'login' =>  array(
            'route' => '/login',
            'paths' => array(
               'module' => 'Auth',
                'controller' => 'Frontend\Auth',
                'action' => 'login'
            ),
            'type' => 'static'
        )
    );

  • Rest - using to create REST Api. Allows to map HTTP methods with controller actions.
    return array(
        'products' =>  array(
            'route' => '/products',
            'paths' => array(
                'module' => 'Product',
                'controller' => 'Frontend\Product'
            ),
            'type' => 'rest',
            'params' => array(
                'actions' => array(
                    '/' =>  array(
                        'index' => Vegas\Http\Method::GET,
                        'create' => Vegas\Http\Method::POST
                    ),
                    '/{id}' =>  array(
                        '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 array(
        'not-found' => array(
            'route' => '',
            'paths' => array(
                '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 array(
    'not-found' => array(
        'route' => '/foo',
        'paths' => array(
            'module' => 'Foo',
            'controller' => 'Frontend\Foo',
            'action' => 'index'
        ),
        'type' => 'default',
        'params' => array(
            '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.