Routing

Walrus use a simple routing system inspired by ROR and Symfony. All your routes must be created with yaml in the config/routes/ directory

All configuration YAML files in walrus are parsed to PHP array, you can create all the files you want in the correct configuration directory

Simple route

A route need a least four information, a name, a path, a controller and an action

    _home:
        path:   'home'
        controller: 'HomeController'
        action: 'run'
  • Line 1: the route name, used to differentiate your routes, the route name is unique
  • Line 2: the route path, need to be a valid url, you don't need to add a slash in the begining
  • Line 3: the controller, a full controller name
  • Line 4: the action, a public method from the controller
Parameters

You can add all the parameter you want, but each parameter must have a unique name and optional parameter must be at the end of the path.
Parameter are written like this :param and optional parameter like this (:param)

Let's add four parameters to our route, with the two last one otpional, of course in this case, the run method should take at least 2 parameter and 2 optional paramater too.

    _home:
        path:   'home/:param1/:param2/(:param3)/(:param4)'
        controller: 'HomeController'
        action: 'run'
Method

The HTTP method can be specified for each route and sanitize your aprameter according the method of your choice. Valid method for routing are GET, POST, PUT and DELETE
By default the method is set to GET.

    _home:
        path:   'home'
        controller: 'HomeController'
        action: 'run'
        method: 'POST'
Simple ACL

To limit the access of a specific route in a simple way, you can use Walrus ACL system. In this example we limit the access to user with the admin role

    _home:
        path:   'home'
        controller: 'HomeController'
        action: 'run'
        acl: 'admin'
Filters

To secure the data input of your route you can use filters, there is two type of filters, the first one let you make type verification on the parameter with a regex and the second let you define a default value.

Require filter example:
In this example we aply the /[\d-]+/ regex pattern to :param1

    _home:
        path:   'home/:param1'
        controller: 'HomeController'
        action: 'run'
        filters:
            require:
                param1: '[\d-]+'

Default filter example:
In this example we aply the hello value to :param1 if it's not specified in the URL

    _home:
        path:   'home/:param1'
        controller: 'HomeController'
        action: 'run'
        filters:
            default:
                param1: 'hello'
Complexe routing

You can of course use everything together to create more intelligent routing

    _home:
        path:   'home/:param1/:param2/:param3/(:param4)'
        controller: 'HomeController'
        action: 'run'
        method: 'GET'
        acl: 'admin'
        filters:
            require:
                param1: '\d+'
                param2: '[A-Z]'
                param3: '\w{4}'
                param4: '\w+'
            default:
                param4: 'hello'
Default page

You can setup default pages for 403 and 404 error codes by naming them _404 and _403