Home >Backend Development >PHP Tutorial >CodeIgniter configuration routes.php usage example analysis, codeigniterroutes_PHP tutorial
This article provides an example analysis of CodeIgniter configuration routes.php usage. Share it with everyone for your reference, the details are as follows:
An array named $route is defined in application/config/routes.php, which is used to set the default route and 404 page and set some matching methods.
The default configuration is as follows:
$route['default_controller'] = "welcome"; $route['404_override'] = '';
default_controller specifies the default controller name, and 404_override specifies the controller name to be called when 404 occurs. Sometimes the parsing may fail, or it may remain on the default page. We can call $this->router to print the currently parsed controller and action names. For example, you can print it in MY_Controller as follows:
var_dump($this->router->fetch_directory()); var_dump($this->router->fetch_class()); var_dump($this->router->fetch_method());
Confirm which controller is parsed, then look at the URL configuration, server configuration, and debug in Router.php and URI.php.
The $route array can also set rewriting rules through wildcards (:num, :any) and regular expressions. Here are some simple examples:
1. Parse http://pc.local/admin/detail_1.htm request to http://pc.local/admin/detail.htm?user_id=1 for processing.
Codeigniter does not support rewriting rules containing query strings. This rule should look like this:
Copy code The code is as follows: $route['admin/detail_(:num)'] = 'admin/detail?user_id=$1';
But it does not actually take effect. The program matches admin/detail?user_id=1 and separates it with "/". The index is 0 for the controller name, and the index for 1 is the method name. That is to say, the above detail?user_id will be =1 is assigned to the method name, and the result is 404 as you can imagine. After understanding the separation principle, you can add a slash after detail to ensure that the class name and method name are correct, such as:
Copy code The code is as follows: $route['admin/detail_(:num)'] = 'admin/detail/?user_id=$1';
But at this time, there is a problem of obtaining parameters. The third parameter will be passed to the method. If you need to use $_GET or $this->input->get, you need to process the parameters, such as:
Copy code The code is as follows: parse_str(ltrim($query_string, '?'), $_GET);
2. The URL form rewriting rules of PATH_INFO are relatively supported. If you want to implement the format http://pc.local/admin/1:
Copy code The code is as follows: $route['admin/(:num)'] = 'admin/detail/$1';
Parameters can only be obtained through paragraphs.
Note: Routes will be run in the order defined. Higher-level routes always take precedence over lower-level routes.
Finally, it is recommended to use CI for routing that can be set up without relying on server configuration.
Readers who are interested in more content related to the CodeIgniter framework can check out the special topic on this site: "Introduction to codeigniter tutorial"
I hope this article will be helpful to everyone’s PHP program design based on the CodeIgniter framework.