URI routing
Generally speaking, a URI string has a unique corresponding controller class/method. The various parts of the URI are the following patterns:
For example, you might want to use this prototype for your URL:
Normally, the second part of the URL represents the method name, but in the above example, it represents a product ID. CodeIgniter can implement this function, allowing users to redirect (remap) URI handlers.
Set your own routing rules
Routing rules are defined in the application/config/routes.php file. In this file, you can see an array named $route, which allows you to define your own routing rules. Definitions can be used in two ways: wildcards or regular expressions
Wildcard
A typical wildcard route looks like this:
In a route, the array key contains the URI to be matched, and the array value contains the destination to which the route will be redirected. In the above example, if the word "product" appears first in the URL parts, and the number (:num) appears in the second part of the URI, the "catalog" class and "product_lookup" method will be used instead (will be redirected).
You can match literal values or use the following two wildcard types:
:num will match a segment containing only numbers.
:any will match any character (can be multiple segments). Can match multiple values, such as:
$route['product/(:any)'] = "catalog/product_lookup/$1/$2/$3/$4/$5"; // Pass every parameter on the entire url to the catalog controller product_lookup method.
Note: Routes will be run in the order defined. Higher-level routes always take precedence over lower-level routes.
Example
Here are some simple examples:
When "product" is the first segment in the URL, if the second segment is a number, it will be redirected to the "catalog" class and the matched content will be passed to the "product_lookup_by_id" method.
Important note: Do not add "/" before or after.
Regular expression
If you like, you can use regular expressions to customize your routing rules. Any valid regular expression is allowed, even reverse references.
Note: If you use backreferences please replace the double backslash syntax with dollar sign syntax (\1 is replaced by $1).
A typical regular expression looks like the following:
In the above example, a 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 will retain two routes:
The first one is the system default route:
This route indicates which controller will be loaded when the URI does not contain the class and controller information to be accessed (that is, when only the root directory is accessed, such as http://localhost/ci). In the above example, the system will load the "welcome" class (controller). You should make sure to set a default route, otherwise your homepage will show a 404 error.
The second one is the route for the 404 page:
This route identifies which controller will be loaded if the requested controller is inaccessible. It is equivalent to overwriting the default 404 error page (that is, providing the function of defining your own 404 page). But it will not affect the show_404() method, which will still load the default error_404.php page located at application/errors/error_404.php.
Important: Reserved routes should be defined before any wildcard or regular expression routes.
//use url helper
$this->load->helper('url');
current_url = current_url();
To achieve this, we can only use pseudo-static. .htaccess configuration rule implementation