Home  >  Article  >  Backend Development  >  PHP Codeigniter 中的_re地图方法简介

PHP Codeigniter 中的_re地图方法简介

WBOY
WBOYOriginal
2016-06-13 13:19:45840browse

PHP Codeigniter 中的_remap方法简介
  在Codeigniter 中,其实有个容易忽略掉的不错的控制器层处理的方法,
那就是remap,这里简单介绍下。
   其实在CI的URL控制方式表示中,比如:
example.com/index.php/blog/comments/

  这样的形式中,blog就是controller,comments就是你的方法了。
又比如参数的传递是这样的:
example.com/index.php/products/shoes/sandals/123

   sandals/123就是传递的两个参数了。
URI 的第二片段决定会调用控制器中的哪个方法。CodeIgniter 允许你使用 _remap() 方法来废除这种规则:

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

注意:如果你的控制器中包含一个名为 _remap() 的方法,那么不管你的 URI 中包含什么,它总会被忽略掉。这个方法会废除掉由 URI 片段来决定哪个方法被调用的规则,允许你重新定义调用方法的规则(方法的路由规则)。

   但问题是,手册上这样说的,会有什么用处呢?其实用处有两个:
1,改变URL,隐藏方法,比如你的应用中,原来的URL方法是:
   http://anmsaiful.net/blog/display_successful_message
   现在想改变显示的方法名为:
    http://anmsaiful.net/blog/successful
    但显示虽然是successful,但实际上是调用存在的display_successful_message

方法,这就要用到_remap方法了。

2 还可以借这个函数做简单的函数方法控制,比如:
    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();
    }
}
    首先取出用户session中的级别 $user_type,然后检查通过方法
validate_access这个用户有无权限调用这个方法($method)
,如果有的话$access_control==true了,否则显示出错信息



1 楼 shirne 2012-04-06  
学习了..
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