Fork me on GitHub

Documentation

Route

The Route component maps incoming URIs to controller actions. A route table is a name-keyed array of route definitions; on a match, the corresponding controller and action are dispatched by the MVC pipeline.

Route Table

Routes are defined in each module's module.config.php under the 'routes' key. Each entry requires a route URI, a controller FQCN, and an action name:

    

    declare(strict_types=1);

    namespace Application;

    use Application\Controller\IndexController;

    return [
        'routes' => [
            'application.home' => [
                'route'       => '/',
                'controller'  => IndexController::class,
                'action'      => 'index',
            ],
        ],
        'view_manager' => [
            'template_map' => [
                'layout/layout' => 'layout/layout.phtml',
                'error/404'      => 'error/404.phtml',
            ],
            'template_path_stack' => __DIR__ . '/../view',
        ],
    ];
    
                

The route table supports any additional keys per entry — they are returned untouched on a match and can be used for middleware flags, permissions, or metadata.

How Matching Works

During the MVC pipeline, getMatchRoute() derives the current URI from $_SERVER (stripping the front-controller directory and query string) and compares it against each registered route. The first match wins.

When a route matches, these pipeline properties become available:

  • $app->matchRoute — the full matched route entry keyed by name
  • $app->namespace — the controller FQCN from the match
  • $app->controllerName — the short controller class name
  • $app->actionName — the matched action (e.g. index)
  • $app->moduleName — the first segment of the namespace

If no route matches, the pipeline sends 404 headers and renders the error/404 template.

URI Resolution

Route names can also be resolved back to URIs via getRouteUri(). This is useful for building links in views or redirect targets:

    

    // Resolve a route name to its URI
    $uri = $route->getRouteUri('application.home');
    // $uri = '/'

    // You can also pass a route table explicitly
    $uri = $route->getRouteUri('blog', [
        'blog' => ['route' => '/blog'],
    ]);
    // $uri = '/blog'
    
                

Redirecting to a Route

The $this->redirect helper in controllers sends a Location header to a named route and terminates the request:

    

    declare(strict_types=1);

    namespace Application\Controller;

    use JiNexus\Mvc\Controller\AbstractController;

    class IndexController extends AbstractController
    {
        public function indexAction(): void
        {
            // 302 (temporary) redirect
            $this->redirect->toRoute('application.home');
        }
    }
    
                

Pass true as the second argument for a 301 (permanent) redirect:

    

    $this->redirect->toRoute('application.home', true);
    // 301 Location: /
    
                

The redirect throws a RouteException if the route name is empty or not found and skips the header if headers have already been sent.