Directors

Directors are helper classes for dealing with complicated routes. To implement a director, create a new class that extends from Core\Director and override its route() method. This method should return an object of type Core\Routing\Route.

application/search/director.php
<?php

namespace Application\Search;

use Core\Routing\Route;

class Director extends \Core\Director
{
    public function route()
    {
        $route = new Route('search');
        $route->optional('at-most-%d-eur', 'max_price')->match('max_price', '[0-9]+');
        $route->optional('page-%d', 'page')->match('page', '[0-9]+');
        
        return $route;
    }
}

You can then use this class instead of a URI in a controller's route() method.

application/search/search.php
public static function route($router)
{
    $router->get(new Director(), 'searchresults');
}

Processing route arguments

You can reduce the amount of arguments passed to your controller by processing them within the director. To do so, implement the shrink_arguments() and expand_arguments() methods.

application/search/director.php
public function shrink_arguments($arguments)
{
    // Fetch search results
    $results = [];
    
    return [
        'results' => $results,
    ];
}

public function expand_arguments($arguments)
{
    $results = $arguments['results'];
    
    return [
        'max_price' => $results->max_price,
        'page' => $results->page,
    ];
}
Edit this page on GitHub Updated at Mon, Feb 14, 2022