Introduction to the _ remap method in PHP Codeigniter

Source: Internet
Author: User
Tags codeigniter

In Codeigniter, there is actually a good way to handle controller layers that are easy to ignore,
This is remap. Here is a brief introduction.
In fact, in the ci url control mode representation, for example:
Example.com/index.php/blog/comments/

In this form, the blog is the controller, and the comments is your method.
For example, the parameter transmission is as follows:
Example.com/index.php/products/shoes/sandals/123

Sandals/123 is the two parameters passed.
The second segment of URI determines the method in the controller. CodeIgniter allows you to use the _ remap () method to abolish this rule:

Public function _ remap ()
{
// Some code here...
}

NOTE: If your controller contains a method named _ remap (), it will be ignored no matter what the URI contains. This method will abolish the rule that the URI fragment determines which method is called and allow you to redefine the rule for calling the method (the routing rule of the method ).

But the question is, what is the use of this in the manual? In fact, there are two functions:
1. Change the URL and hide the method. For example, in your application, the original URL method is:
Http://anmsaiful.net/blog/display_successful_message
Now you want to change the display method name:
Http://anmsaiful.net/blog/successful
However, although it is successful, it is actually the display_successful_message that exists in the call.

The _ remap method is required.

2. You can also use this function for simple function method control, such:
Public function _ remap ($ method, $ params = array ())
{
$ User_type = $ _ SESSION ['user _ type'];
$ Access_control = $ this-> validate_access ($ user_type, $ method );
If ($ access_control ){
$ This-> $ method ();
}
Else {
$ This-> show_message ();
}
}
First retrieve the level $ user_type in the user session, and then check the Pass Method
Validate_access whether the user has the permission to call this method ($ method)
If yes, $ access_control = true; otherwise, the error message is displayed.


From jackyrong

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.