CodeIgniter configuration-routes. php usage instance analysis-php instance

Source: Internet
Author: User
This article mainly introduces the routes of CodeIgniter configuration. php usage, combined with the instance form analysis of routes. common configuration parameters in php are described as follows. php usage. We will share this with you 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, and to 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 name of the Controller called when 404 appears. Sometimes the resolution may fail, or it is always on the default page. We can call $ this-> router to print the name of the controller and Acion currently parsed. For example, you can print the following in MY_Controller:

var_dump($this->router->fetch_directory());var_dump($this->router->fetch_class());var_dump($this->router->fetch_method());

Determine which controller to parse, and then check the URL configuration, server configuration, and debug it in Router. php and URI. php.

$ Route Arrays can also be set with wildcards (: num,: any) and regular expressions. Below are some simple examples:

1. Resolve the http://pc.local/admin/detail_1.htm request to the http://pc.local/admin/detail.htm? User_id = 1.
Codeigniter does not support rewrite rules that contain query strings. This rule should look like this:

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? After user_id = 1, use "/" to separate them. If the index is 0, the Controller name is used. If the index is 1, the method name is used? User_id = 1 is assigned to the method name, and the result is 404. After clarifying the separation principle, you can add a slash next to detail to ensure that the class name and method name are correct, for example:

The Code is as follows:

$ Route ['admin/detail _ (: num) '] = 'admin/detail /? User_id = $1 ';


However, there is a problem with obtaining parameters. The third parameter will be passed to the method, if you want to use $ _ GET or $ this-> input-> get, you also need to process the parameters, such:

The Code is as follows:

Parse_str (ltrim ($ query_string ,'? '), $ _ GET );


2. URL-based rewriting rules for PATH_INFO are supported. To implement the format http://pc.local/admin/1:

The Code is as follows:

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


Parameters can only be obtained through paragraphs.

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

Finally, we recommend that you use CI to set the routes that can be configured by using CI instead of the server configuration.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.