CI framework source code Study (route analysis)-php tutorial

Source: Internet
Author: User
Tags codeigniter
In the previous article on CI framework source code Study (route parsing), we mentioned that in CodeIgniter. in php, the controllers and action corresponding to the url you access are parsed, and then the call_user_func_array () method is called to enter the action. How can we get the corresponding relationship here, this is what we will discuss now.

1. CodeIgniter. php

We first came to CodeIgniter. php and found that this is actually implemented in the Router class, the following source code:

$ RTR = & load_class ('router ', 'core', isset ($ routing )? $ Routing: NULL );

$ Class = ucfirst ($ RTR-> class );

$ Method = $ RTR-> method;

2. Router. php

Let's go to Router. php to check whether the constructor In the Router class is centered here:

$ This-> _ set_routing ();

_ Set_routing:

$ _ D = $ this-> config-> item ('Directory _ trigger ');

$ This-> set_directory ($ _ d );

$ _ C = trim ($ this-> config-> item ('controller _ trigger '));

$ This-> set_class ($ _ GET [$ _ c]);

$ _ F = trim ($ this-> config-> item ('function _ trigger '));

$ This-> set_method ($ _ GET [$ _ f]);

$ This-> _ parse_routes ();

Obtain directory, class, and method from the global variable _ GET array respectively. Their triggers can be configured in the configuration file.Of course, this is when enable_query_strings is set to true.. Next we enter the most critical method _ parse_routes method:

In the CI route configuration, We can configure the following:

$ Route ['products'] ['put'] = 'product/insert ';

This is because the code in _ parse_routes:

$ Http_verb = isset ($ _ SERVER ['request _ method'])? Strtolower ($ _ SERVER ['request _ method']): 'cl ';

$ Val = array_change_key_case ($ val, CASE_LOWER );

If (isset ($ val [$ http_verb]) {

$ Val = $ val [$ http_verb];}

Else {continue ;}

There are several HTTP verbs (GET, PUT, POST, DELETE, PATCH). We can configure this in the Configuration:

$ Route ['product/(: num) '] = 'catalog/product_lookup_by_id/$1 ';

This is because the code in _ parse_routes:

$ Key = str_replace (array (': any',': num'), array ('[^/] +', '[0-9] + '), $ key );

Callback functions can be configured in route configuration, for example:

$ Route ['products/([a-zA-Z] +)/edit/(\ d +) '] = function ($ product_type, $ id) {return 'catalog/product_edit /'. strtolower ($ product_type ). '/'. $ id ;};

This code is available:

If (! Is_string ($ val) & is_callable ($ val )){

Array_shift ($ matches );

$ Val = call_user_func_array ($ val, $ matches );

}

General Configuration:

Else if (strpos ($ val ,')! = FALSE & strpos ($ key ,'(')! = FALSE ){

$ Val = preg_replace ('# ^'. $ key. '$ #', $ val, $ uri );

}

Finally, the following code is called:

$ This-> _ set_request (array_values ($ this-> uri-> segments ));

_ Set_request Method

$ This-> set_class ($ segments [0]);

If (isset ($ segments [1]) {

$ This-> set_method ($ segments [1]);

} Else {

$ Segments [1] = 'index ';

}

Set the class and method, and the class and method we need in CodeIgniter. php.

3. Uri. php

Here we will talk about the above uri's path:

$ Uri = implode ('/', $ this-> uri-> segments );

Go to the Uri class to check whether:

The core method is _ parse_request_uri. In the method body,

$ Uri = parse_url ('HTTP: // dummy'. $ _ SERVER ['request _ URI ']);

$ Query = isset ($ uri ['query'])? $ Uri ['query']: '';

$ Uri = isset ($ uri ['path'])? $ Uri ['path']: '';

Return $ this-> _ remove_relative_directory ($ uri );

Parse_url is used to parse the path and query in the url, and then remove the relative path.

$ This-> uri-> segments the member variable stores the array after explode ('/', trim ($ uri, assign a value to the _ set_uri_string method.

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.