Upgrade Routing
Documentations
What has been changed
In CI4 the Auto Routing is disabled by default.
In CI4 the new more secure Auto Routing (Improved) is introduced.
In CI4 the routing is no longer configured by setting the routes as array.
The Wildcard
(:any)
In CI3 will be the Placeholder(:segment)
in CI4. The(:any)
in CI4 matches multiple segements. See URI Routing.
Upgrade Guide
If you use the Auto Routing in the same way as CI3, you need to enable Auto Routing (Legacy).
You have to change the syntax of each routing line and append it in app/Config/Routes.php. For example:
$route['journals'] = 'blogs';
to$routes->add('journals', 'Blogs::index');
. This would map to theindex()
method in theBlogs
controller.$route['product/(:any)'] = 'catalog/product_lookup';
to$routes->add('product/(:segment)', 'Catalog::productLookup');
. Don’t forget to replace(:any)
with(:segment)
.$route['login/(.+)'] = 'auth/login/$1';
to$routes->add('login/(.+)', 'Auth::login/$1');
Note
For backward compatibility,
$routes->add()
is used here. But we strongly recommend to use HTTP verb Routes like$routes->get()
instead of$routes->add()
for security.
Code Example
CodeIgniter Version 3.x
Path: application/config/routes.php:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
// ...
$route['posts/index'] = 'posts/index';
$route['teams/create'] = 'teams/create';
$route['teams/update'] = 'teams/update';
$route['posts/create'] = 'posts/create';
$route['posts/update'] = 'posts/update';
$route['drivers/create'] = 'drivers/create';
$route['drivers/update'] = 'drivers/update';
$route['posts/(:any)'] = 'posts/view/$1';
CodeIgniter Version 4.x
Path: app/Config/Routes.php:
<?php
use CodeIgniter\Router\RouteCollection;
/**
* @var RouteCollection $routes
*/
$routes->get('/', 'Home::index');
$routes->add('posts/index', 'Posts::index');
$routes->add('teams/create', 'Teams::create');
$routes->add('teams/update', 'Teams::update');
$routes->add('posts/create', 'Posts::create');
$routes->add('posts/update', 'Posts::update');
$routes->add('drivers/create', 'Drivers::create');
$routes->add('drivers/update', 'Drivers::update');
$routes->add('posts/(:segment)', 'Posts::view/$1');
Note
For backward compatibility, $routes->add()
is used here. But we
strongly recommend to use HTTP verb Routes like
$routes->get()
instead of $routes->add()
for security.