Analysis of routes. php Usage instance in CodeIgniter configuration, codeigniterroutes_PHP tutorial

Source: Internet
Author: User
CodeIgniter configuration-routes. php Usage instance analysis, codeigniterroutes. Analysis of routes. php Usage examples of CodeIgniter configuration. in this document, codeigniterroutes analyzes the routes. php Usage of CodeIgniter configuration. For your reference, the details are as follows: Analysis of routes. php Usage instance in ap CodeIgniter configuration, codeigniterroutes

This article analyzes the usage of routes. php in CodeIgniter configuration. 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.