RU EN
Get started

Documentation MVC

Routing

How a URL becomes a module, controller and action. Custom routes with parameters.

The default route

For regular pages you don't need to describe routes at all. The URL path is split into three parts — module, controller and action. Missing parts default to index:

URLModuleControllerAction
/indexindexindex
/aboutaboutindexindex
/shop/cartshopcartindex
/shop/cart/clearshopcartclear

From these three parts the router builds the file path, the class name and the method name:

Mapping
/shop/cart/clear

file:    site/modules/shop/controllers/cartController.php
class:   ShopCartController          // ucfirst(module) . ucfirst(controller) . 'Controller'
method:  clearAction()

Important details:

  • The query string (?page=2) isn't part of the parsing and is available as usual via $_GET.
  • Everything except Latin letters and digits is stripped from each part: /my-shop becomes the module myshop. Use custom routes for URLs with hyphens.
  • Anything after the third part is ignored: /shop/cart/clear/5 is the same clearAction().
  • The controller file is looked up in site/modules first, then in olala/modules. This lets you override a system controller with your own.
  • If the controller file isn't found, the router responds with 404 Not Found.

If the controller file exists but the action method doesn't (for example, /shop/cart/unknown), PHP throws a fatal error. Avoid typos in links, and use custom routes for “risky” URLs.

Custom routes

Custom routes are defined in config.php with router::addCustomRoute(). URL parts starting with a colon are parameters: their values end up in $_GET.

config.php
<?php

use olala\router;

// /shop/15/red-sneakers → CatalogItemController::indexAction()
router::addCustomRoute('/shop/:id/:name', [
    'module'     => 'catalog',
    'controller' => 'item',
    'action'     => 'index',
]);

// /news/2026 → NewsIndexController::yearAction()
router::addCustomRoute('/news/:year', [
    'module'     => 'news',
    'controller' => 'index',
    'action'     => 'year',
]);
site/modules/catalog/controllers/itemController.php
<?php

use olala\controller;

class CatalogItemController extends controller {
    function indexAction() {
        $id   = (int)$_GET['id'];   // 15
        $name = $_GET['name'];      // 'red-sneakers'

        return $this->render('item', ['id' => $id, 'name' => $name]);
    }
}

Matching rules

  • A route must match the whole path: /news/:year won't match /news/2026/05.
  • A parameter may contain Latin letters, digits, hyphens and underscores ([0-9a-z_-]+, case-insensitive).
  • Routes are checked in the order they were added, and the first match wins. Add more specific rules before general ones.
  • If no route matches, the default route is used.

Module, controller and action from the URL

If you leave out module, controller or action in the array, the value is taken from the URL parameter of the same name. This is how the framework's system route works:

config.php
// /system/view/render → SystemViewController::renderAction()
router::addCustomRoute('/system/:controller/:action', [
    'module' => 'system',
]);

// /api/users/list → ApiUsersController::listAction()
router::addCustomRoute('/api/:controller/:action', [
    'module' => 'api',
]);

Extra route data

The whole parameter array of the matched route is stored in router::$params. You can put anything in it — for example, to use a single page action for several URLs:

PHP
router::addCustomRoute('/delivery', ['module' => 'pages', 'controller' => 'index', 'action' => 'page', 'slug' => 'delivery']);
router::addCustomRoute('/payment',  ['module' => 'pages', 'controller' => 'index', 'action' => 'page', 'slug' => 'payment']);

// in PagesIndexController::pageAction():
return $this->render(router::$params['slug']);

What's known about the current request

Property / methodValue
router::$modulemodule name, e.g. 'blog'
router::$controllercontroller name
router::$actionmethod name with the suffix, e.g. 'showAction'
router::$paramsparameter array of the matched custom route
router::$defaultRoutetrue if the default route was used
router::$controllerInstancethe current controller object
router::getHeader($name, $default)a request header
request::getPath()the path without the query string (olala\modules\request)

This comes in handy, for example, for highlighting the active menu item in a layout:

site/layouts/default/index.php
<?php use olala\router; ?>
<a href="/blog" <?= router::$module === 'blog' ? 'class="active"' : '' ?>>Blog</a>

Disabling the default route

Sometimes a controller should be reachable only through its pretty URL — so the page has no duplicates like /catalog/item/index. Add a $noDefaultRoute property to the controller: a visitor coming in via /module/controller/action will be redirected to the home page.

PHP
class CatalogItemController extends controller {
    public $noDefaultRoute = true;

    function indexAction() { /* ... */ }
}