CodeIgniter Framework URL Routing Summary

  • 2021-07-18 07:26:13
  • OfStack

URI routing

1 Generally speaking, the URI string has a controller (controller) class/method corresponding to its only 1. Each part of URI is the following pattern (pattern):


example.com/class/function/id/

In one example, however, you might want to redirect this relationship to call a different class/method (class/function) instead of the corresponding URL11.

For example, you may want to make your URL adopt this prototype (prototype):


example.com/product/1/
example.com/product/2/
example.com/product/3/
example.com/product/4/

1 In general, the second part of URL represents the method name, but in the above example, it represents the ID of 1 product. CodeIgniter implements this functionality, allowing users to redirect (remap) URI handlers.

Set your own routing rules

Routing rules are defined in the file application/config/routes. php. In this file, you can see an array named $route, which allows you to define your own routing rules. You can define it in two ways: a wildcard (wildcards) or a regular expression (Regular Expressions)

Wildcard character

A typical wildcard route looks like this:


$route['product/(:num)'] = "catalog/product_lookup";

In a route, the key of the array contains the matched URI, and the value of the array contains the destination to which the route will be redirected. In the above example, if the word "product" appears in the first part of URL and the number (: num) appears in the second part of URI, the "catalog" class and the "product_lookup" method will be used instead (to be redirected).

You can match the value of the text or use the following two wildcard types:

: num will match a number-only segment (segment).

any will match any character (can be multiple segment segments). Multiple values can be matched, such as:

$route ['product/(: any)'] = "catalog/product_lookup/$ 1/$2/$3/$4/$5"; //Pass every 1 parameter on the entire url to the product_lookup method under the catalog controller.

Note: Routes will run in a defined order. High-level routes always take precedence over low-level routes.

Example

Here are a few simple examples:


$route['journals'] = "blogs";

If the first segment (class name) of URL is the keyword "journals", it will be redirected to the "blogs" class for processing.


$route['blog/joe'] = "blogs/users/34";

If the first two segments of URL are "blog" and "joe", they will be redirected to the "users" method of the "blogs" class for processing. ID "34" is set as a parameter.


$route['product/(:any)'] = "catalog/product_lookup";

When "product" is the first segment in URL, whatever the second segment is, it is redirected to the "product_lookup" method of the "catalog" class.


$route['product/(:num)'] = "catalog/product_lookup_by_id/$1";

When "product" is the first segment in URL, if the second segment is a number, it will be redirected to the "catalog" class and the matching content will be passed to the "product_lookup_by_id" method.

Important note: Do not add "/" before or after it.

Regular expression

You can use regular expressions to customize your routing rules if you like. Any valid regular expressions are allowed, even reverse references.

Note: If you use reverse references, replace double backslash syntax with dollar sign syntax (\\ 1 with $1).

1 A typical regular expression looks like the following:


   $route['products/([a-z]+)/(\d+)'] = "$1/id_$2";

In the above example, an URI similar to products/shirts/123 will be replaced by calling the id_123 method of the shirts controller class.

You can also mix wildcards with regular expressions.

Routes reserved by the system

The system preserves two routes:

The first is the system default route:


$route['default_controller'] = 'welcome';

This route indicates which controller will be loaded when URI contains no class and controller information to be accessed (that is, only the root directory is accessed, such as http://localhost/ci). In the above example, the system will load the class "welcome" (controller). You should make sure to set a default route, otherwise your home page will display 404 errors.

The second is the route for 404 pages:


$route['404_override'] = '';

This route identifies which controller will be loaded if the requested controller cannot be accessed. This is equivalent to overriding the default 404 error page (that is, providing the ability to define your own 404 page). However, it does not affect the show_404 () method, which still loads the default error_404. php page at application/errors/error_404. php.

Important: Reserved routes should be defined before all wildcard or regular expression routes.


Related articles: