Router

Chapter 6

Introduction


Madeam's router is the module that handles the URL you enter when visiting a website and then dispatches priority to a controller.

The way the router works is it tries to match the URL to a pattern you've defined in your routes.php file.

The default route:

<?php
$router->connect(':controller/:action/:id.:format');
?>

Syntax

Slashes and periods split the pattern into parameters. You can give each parameter a name by adding a colon (:) and then the name of the parameter.

Main Parameters

In order for the router to properly dispatch to a controller Madeam needs 3 main parameters which are "controller", "action" and "format".

Controller is the name of the controller that you want to dispatch to and action is the name of the action inside the controller you want to run. Format allows you to change the type of output but by default it's html.

The 3 main parameters all have default values which can be defined in config/setup.php. It's recommended that you leave them as their defaults.

Creating a New Route


To create a new route use the router's connect() method. The connect() method takes 2 parameters. The first parameter is the pattern and the second is the values of the parameters.

<?php
// set the value of the controller to "testing"
$router->connect(':controller/:action/:id', array('controller' => 'testing'));

// you could also type...
$router->connect('testing/:action/:id', array('controller' => 'testing');

// or...
$router->connect('whatever/:action/:id', array('controller' => 'testing');
?>
Previous Chapter