Class Misago\ActionController\Routing\Routes

Routing is what connects HTTP requests to your application's controllers, parsing actions an parameters too.

You do configure your application's routes in config/routes.php. A basic route config file looks like this:

$map = Misago\ActionController\Routing\Routes::draw();

# basic route: connects /login to AccountsController::login()
$map->connect('login', array(
  ':controller' => 'accounts',
  ':action'     => 'login'
));

# landing page: / => HomeController::index()
$map->root(array(':controller' => 'home'));

# default route
$map->connect(':controller/:action/:id.:format'); 

You may use a different default route, for instance:

$map->connect(':controller/:action.:format');
$map->connect(':controller/:id/:action.:format'); 

Named routes

You may configure named routes, which will not only recognize the route but will also create some helper functions. For instance:

$map->named('purchase', 'products/:id/purchase',
  array(':controller' => 'order', ':action' => 'purchase')); 

This will create the following functions: purchase_path() and purchase_url(), which you may use like this:

purchase_path(1);
purchase_path(array(':id' => 1));
purchase_path(new Product(1)); 

Requirements (Regular Expressions)

You may check parameters by using regular expressions. A route that doesn't match requirements isn't matched. In this example :id must be an integer:

$map->connect('/posts/:id', array(':controller' => 'posts',
  'requirements' => array(':id' => '\d+'))); 

Conditions

You may add conditions to routes. At the moment only the HTTP method can be a condition. For instance the following route will only be used on a POST request:

$this->connect('login', array(':controller' => 'accounts', ':action' => 'login',
  'conditions' => array('method' => 'POST'))); 

Using named routes with conditions, returned URL will be a Misago\ActionController\Routing\Url or Misago\ActionController\Routing\Path object, that will be transparently handled by view helpers to generate links and forms that will use the correct HTTP method.

IMPROVE: cache routes using APC.

Inheritence

Extends:
ResourceRoutes

Methods

Public static methods

collect()

Returns the full list of routes.

draw()

Singleton

recognize($request)

Recognizes the route for a request, and returns a controller.

Public instance methods

connect($path, $mapping=array())

Connects a path to a mapping.

named($name, $path, $mapping=array())

Connects a path to a mapping, giving the route a name.

ns($name, $closure)

Sometimes it's nice to separate some resources into a particular namespace. For instance:

$map->ns('admin', function($admin) {
  $admin->resource('product');
} 

This will require the controller Admin\ProductsController (as app/controllers/Admin/ProductsController.php) and will generate the following named routes:

admin_products      admin/products          Admin\ProductsController::index()
new_admin_product   admin/product/new       Admin\ProductsController::neo()
show_admin_product  admin/product/:id       Admin\ProductsController::show()
edit_admin_product  admin/product/:id/edit  Admin\ProductsController::edit()
etc. 

reset()

Empties routes.

root(array $mapping)

Connects the homepage.