Home > Backend Development > PHP Tutorial > CodeIgniter configuration routesphp usage example analysis

CodeIgniter configuration routesphp usage example analysis

WBOY
Release: 2016-07-29 09:06:04
Original
1014 people have browsed it

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'] = '';

Copy after login

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());

Copy after login

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';


But it does not actually take effect. The program matches admin/detail?user_id=1 and is separated by "/". The index is 0 for the controller name and the index for 1 is the method. Name, that is, the above detail?user_id=1 will be assigned to the method name. As you can imagine, the result will be 404. 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 the code The code is as follows:

$route['admin/detail_(:num)'] = 'admin/detail/?user_id=$1';


But now there is a problem with getting parameters. The third parameter will be passed to the method. If you need to use $_GET or $this->input-> Get acquisition also needs to process parameters, such as:

Copy code The code is as follows:

parse_str(ltrim($query_string, '?'), $_GET);


2. Repeat the URL form of PATH_INFO Writing rules is quite supportive. If you want to implement the format http://pc.local/admin/1:

Copy the code The code is as follows:

$route['admin/(:num)'] = 'admin/detail/$1 ';


parameters can only be obtained through paragraphs.

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.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template