Example analysis of routes.php usages of codeigniter configuration _php Example

Source: Internet
Author: User
Tags codeigniter

The routes.php usage of codeigniter configuration is analyzed in this paper. Share to everyone for your reference, specific as follows:

An array named $route is defined in application/config/routes.php to set the default route and 404 pages and to set up some matching methods.

The default configuration is as follows:

$route [' default_controller '] = "welcome";
$route [' 404_override '] = ';

DEFAULT_CONTROLLER Specifies the default controller name, 404_override specifies the name of the controller that is invoked when 404 occurs. Sometimes the resolution is unsuccessful, or the default page is always available, and we can call $this->router to print out the currently resolved controller and acion name. 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 resolve under, and then look at the URL configuration, server configuration, and can be debugged in router.php and uri.php.

$route arrays can also be set to override rules by wildcard characters (: num, any), and the following are some simple examples:

1, the http://pc.local/admin/detail_1.htm request resolution to http://pc.local/admin/detail.htm?user_id=1 processing.
CodeIgniter does not support overriding rules that contain query strings, and this rule should look like this:

Copy Code code as follows:
$route [' Admin/detail_ (: num) '] = ' admin/detail?user_id=$1 ';

But actually not effective, the program matching to admin/detail?user_id=1 after the "/" separated, the index of 0 for the controller name, index 1 for the method name, that is, will assign the above detail?user_id=1 to the method name, the result can be imagined 404. After figuring out the principle of separation, you can add a slash behind the detail to ensure that the class name and method name are correct, such as:
Copy Code code as follows:
$route [' Admin/detail_ (: num) '] = ' admin/detail/?user_id=$1 ';

However, there is also the problem of obtaining parameters, the third parameter is passed to the method, if you need to use $_get or $this->input->get fetch also need to process the parameters, such as:
Copy Code code as follows:
Parse_str (LTrim ($query _string, '? '), $_get);

2, the Path_info URL form rewrite rules or more support. To implement HTTP://PC.LOCAL/ADMIN/1 this format:
Copy Code code as follows:
$route [' admin/(: num) '] = ' admin/detail/$1 ';

The argument can only be obtained by means of a paragraph.

Note: Routes will run in the order that they are defined. High-level routing always takes precedence over low-level routes.

Finally, the route that can be set using CI is recommended for use with CI to set, not dependent on server configuration.

More about the CodeIgniter framework interested readers can view the site topic: "CodeIgniter Introductory Course"

I hope this article will help you with the PHP program design based on CodeIgniter framework.

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.