Home >Backend Development >PHP Tutorial >Introduction to the _remap method in PHP Codeigniter_PHP tutorial

Introduction to the _remap method in PHP Codeigniter_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:50:19946browse

In Codeigniter, there is actually a good controller layer processing method that is easy to overlook,
That is remap, which is briefly introduced here.
In fact, in the URL control method of CI, for example:
example.com/index.php/blog/comments/

In this form, blog is the controller and comments are your methods.
Another example is the passing of parameters like this:
example.com/index.php/products/shoes/sandals/123

sandals/123 are the two parameters passed.
The second fragment of the URI determines which method in the controller will be called. CodeIgniter allows you to override this rule using the _remap() method:

public function _remap()
{
// Some code here...
}

Note: If your controller contains a method called _remap(), it will always be ignored regardless of what your URI contains. This method removes the rule that URI fragments determine which method is called, allowing you to redefine the rules for calling methods (method routing rules).

But the question is, what is the use of what the manual says? In fact, it has two uses:
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 the method I want to change the display is named:
http://anmsaiful.net/blog/successful
But although the display is successful, it actually calls the existing display_successful_message

method, this requires the _remap method.

2 You can also use this function to perform simple function method control, such as:
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, take out the level $user_type in the user session, and then check the passing method
validate_access Does this user have permission to call this method ($method)
, if there is $access_control==true, otherwise an error message will be displayed


Excerpted from jackyrong

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478284.htmlTechArticleIn Codeigniter, there is actually a good controller layer processing method that is easily overlooked, and that is remap. Here is a brief introduction. In fact, in the expression of CI's URL control method, for example:...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn