Routing rules are defined in your /app/Config/Routes.php file.

There is a one-to-one relationship between a URL string and its corresponding controller class/method. The segments in a URI normally follow this pattern:

example.com/class/function/id/

Syntax:

$routes->add('from', 'to', $options);

In some cases, you may want to remap this relationship so that a different class/method can be called instead of the one corresponding to the URL.

To learn how to implement routers on a real-world project, we will assume that we are creating an application for managing contact details. The following table shows the URLs that will be working with.

No.URLRouteControllerMethod
1/$route->add(‘Contacts::index’)Contactsindex
2/contacts$route->add(‘Contacts::list’)Contactslist
3/contacts/add$route->add(‘Contacts::contacts/add’)Contactsadd
4/contacts/create$route->post(‘Contacts::contacts/create’)Contactsadd
5/contacts/edit/id$route->get(‘Contacts::contacts/edit/(:num)’)Contactsedit
6/contacts/update$route->post(‘Contacts::contacts/update’)Contactsupdate
7/contacts/delete/id$route->get(‘Contacts::contacts/delete/(:num)’)welcomedelete

 

Create URLs for Applications

create routes for our project. Open /app/Config/Routes.php

$routes->setDefaultController('Home');
$routes->setDefaultMethod('index');
$routes->add('/', 'Contacts::index');
$routes->add('contacts/list', 'Contacts::list');
$routes->add('contacts/add', 'Contacts::add');
$routes->post('contacts/create', 'Contacts::create');
$routes->get('contacts/edit/(:any)', 'Contacts::edit/$1');
$routes->post('contacts/update', 'Contacts::update');
$routes->get('contacts/delete/(:any)', 'Contacts::delete/$1');

The following table shows the respective URLs obtained from the above-defined routes

No.RouteURL
1$routes->add(‘/’, ‘Contacts::index’);http://localhost
2$routes->add(‘contacts/list’, ‘Contacts::list’)http://localhost/contacts/list
3$routes->add(‘contacts/add’, ‘Contacts::add’);http://localhost/contacts/add
3$routes->post(‘contacts/create’, ‘Contacts::create’);http://localhost/contacts/create
4$routes->get(‘contacts/edit/(:any)’, ‘Contacts::edit/$1’);http://localhost/contacts/edit/1
5$routes->post(‘contacts/update’, ‘Contacts::update’);http://localhost/contacts/update/1
6$routes->get(‘contacts/delete/(:any)’, ‘Contacts::delete/$1’);http://localhost/contacts/delete/1

 

Directory structure

/app
Views
	+ contacts
		- add.php
		- list.php
		- edit.php

	+ innerpages
		- header.php
		- footer.php
		- template.php
	- home.php

 

Global Options

The methods for creating a route (add, get, post, resource, etc.) can take an array of options that can modify the generated routes or further restrict them. The $options an array is always the last parameter:

$routes->get('from', 'to', $options);
$routes->post('from', 'to', $options);
$routes->put('from', 'to', $options);
$routes->head('from', 'to', $options);
$routes->options('from', 'to', $options);
$routes->delete('from', 'to', $options);
$routes->patch('from', 'to', $options);
$routes->match(['get', 'put'], 'from', 'to', $options);
$routes->resource('photos', $options);
$routes->map($array, $options);
$routes->group('name', $options, function());