Routes

In the Quantum framework the routes are done very simple way to map the URL's to Controller actions.

return function($route) {
    $route->add('', 'GET', 'MainController', 'index');
    $route->add('about', 'GET', 'MainController', 'about');
};
        

The add() method of the $route object accepts four arguments:

  • The URI
  • The HTTP method which can be: GET, POST, PUT, PATCH, DELETE or several combination of those
  • The controller name
  • The action name

Routes should be defined in routes.php under Config directory, but due to the modular nature of the framework, routes can be placed separately on each modules Config directory.

/modules/
  • Main/
    • Config/
      • routes.php
    • Controllers/
    • Middlewares/
    • Models/
    • ...
  • Admin/
    • Config/
      • routes.php
    • Controllers/
    • Middlewares/
    • Models/
    • ...

It's possible to pass arguments with GET routes, there are 3 types of arguments allowed:

  • [:num] - Only numbers are allowed
  • [:alpha] - Only letters are allowed
  • [:any] - Numbers, letters and other simboles

Additionally you can defined length of the parameter:

  • [:num:2] - Only numbers with 2 lenght are allowed
  • [:alpha:5] - Only letters with 5 length are allowed
  • [:any:3] - Numbers, letters and other simboles with 3 length are allowed

You can make the route parameter optional by adding ? at the end:

  • [:any]? - The argument can be anything or nothing

Example:

return function($route) {
    $route->add('', 'GET', 'PostController', 'index');
    $route->add('docs/[:num:2]', 'POST', 'PostController', 'create');
    $route->add('docs/[:any:4]/[:alpha]', 'GET', 'PostController', 'artilce');
};

        

It's possible to have several methods defined in oue route seperated by |, that way the route will be fired if request is sent on any of that methods:

return function($route) {
    $route->add('docs/[:num:2]', 'POST|PUT', 'PostController', 'create');
    $route->add('docs/[:any:2]', 'DELETE', 'PostController', 'delete');
};

        

Route Groups

Grouping allows the routes to be organized into one logical scope like so:

return function($route) {
    $route->group('post', function($route) {
        $route->add('posts', 'GET', 'MainController', 'getPosts');
        $route->add('post/[:num]', 'GET', 'MainController', 'getPost');
        $route->add('post/amend/[:num]?', 'POST|PUT', 'MainController', 'amendPost');
    });
};

        

Route Middlewares

It's possible to apply middlewares to any route or route group like this:

return function($route) {
    $route->add('', 'GET', 'MainController', 'index');
    $route->add('about', 'GET', 'MainController', 'about');

    $route->group('post', function($route) {
        $route->add('posts', 'GET', 'MainController', 'getPosts');
        $route->add('post/[:num]', 'GET', 'MainController', 'getPost');
        $route->add('post/amend/[:num]?', 'POST|PUT', 'MainController', 'amendPost')->middlewares(['editor']);
    })->middlewares(['blogger']);
};

        
<< Prev Next >>