Routing

This section explains routing mechanisms into a ModularApplication.

A route can be seen like a request that have to be dispatched to a controller action.

A route is a string identifier corresponding to a module, a controller and an action, which is a controller’s method suffixed by Action. A route is structured like this :

<moduleId>[/<subModuleId>]/<controllerId>/<actionId>.

Example: The request URI /hello can correspond to the route site/default/hello which means that the application will dispatch the route to the method helloAction in the controller DefaultController in the module site.

Configuration

Routes configuration consumed by Piko\Router consists only to an associative array where keys are uri paths and values are their corresponding routes.

if the uri path scheme is structured like a route (moduleId/controllerId/acionId), it doesn’t need configuration.

Example of configuration:

<?php
return [
    // ...
    'components' => [
        'Piko\Router' => [
            'construct' => [
                [
                    'routes' => [
                        //...
                        '/hello' => 'site/default/hello',
                    ],
                ]
            ]
        ],
        // ...
    ]
];

Request parameters

It is possible to retrieve parameters from URI. In the configuration, you can specify parameters in the URI prefixed by a colon character :. These parameters can be then retrieved in controller’s action methods.

For instance, to retrieve an username from the URI:

'routes' => [
    //...
    '/user/:username' => 'user/default/profile',
]

The request /user/:username will be routed to the action profile in the controller default from the user module . The username can be then retrieved as argument of the profile action method:

namespace app\modules\user\controllers;
use Piko\Controller;

class DefaultController extends Controller
{
    public function profileAction(string $username)
    {
        return "Hello $username";
    }
}

With this example, the uri request /user/john will return Hello john.

Dynamic routes

Internal routes can be dynamcally generated by request uri parameters :

'routes' => [
    //...
    '/some/:action' => 'site/default/:action',
]
namespace app\modules\site\controllers;
use Piko\Controller;

class DefaultController extends Controller
{
    //...

    public function timeAction()
    {
        return date('h:i:s');
    }
}

With this example, the uri request /some/time will return something like 12:32:42.

Retrieve URL from route

Piko\Router offers the ability to reverse a route to its uri using the getUrl method:


/** Configuration
'routes' => [
    //...
    '/user/:username' => 'user/default/view',
]
*/

/* @var $router Piko\Router */

echo $router->getUrl('user/default/view', ['username' => 'johnny'];

// will output '/user/johnny'

Inside a controller or view script

Piko\Controller have a getUrl method, implemented as a behavior. It is a proxy to Router::getUrl.

The Controller automatically inject this method as behavior in the Piko\View. So it’s also possible to call $this->getUrl($route) inside view scripts.