2 examples of _remap of in CodeIgniter framework

  • 2021-01-22 04:55:33
  • OfStack

1. CodeIgniter manual related introduction
Segment 2 of 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 named _remap(), it will be ignored regardless of what your URI contains. This method does away with the rule that the ES11en fragment determines which method is called, allowing you to redefine the rules for calling methods (the routing rules for methods).
Either through example. com/index. php blog/to invoke _remap () method, if _remap parameters (), add parameters, after/call specific code.
2. Usage of 2 cases
But the question is, what's the use of what the manual says? There are actually two uses:

1. Change URL to hide methods. For example, in your application, the original URL method is:

example.com/index.php/blog/say

Now we want to change the display method name:
example.com/index.php/blog/hello

But it shows that although it is hello, it is actually calling the existing say method
2. You can also use this function to do simple function method permission 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();  
    }  
}

validate_access ($access_control) ==true ($user_type) = validate_access ($method) ==true ($access_control);


Related articles: