This article analyzes the usage of routes.php in CodeIgniter configuration with an example. Share it with everyone for your reference, the details are as follows:
application/config/routes.php defines an array named $route, which is used to set the default route and 404 page, as well as 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());
Make sure which controller is parsed, and 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. Request http://pc.local/admin/detail_1.htm Parse 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 the code The code is as follows:
$route['admin/detail_(:num)'] = ' admin/detail?user_id=$1';
Copy the code The code is as follows:
$route['admin/detail_(:num)'] = 'admin/detail/?user_id=$1';
Copy code The code is as follows:
parse_str(ltrim($query_string, '?'), $_GET);
Copy the code The code is as follows:
$route['admin/(:num)'] = 'admin/detail/$1 ';
Note: Routing will run in the order defined. Higher-level routing always takes precedence over lower-level routing.
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.
The above introduces the example analysis of routesphp usage in CodeIgniter configuration, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.