一、CodeIgniter手册相关介绍
URI 的第二片段决定会调用控制器中的哪个方法。CodeIgniter 允许你使用 _remap() 方法来废除这种规则:
复制代码 代码如下:
public function _remap()
{
// Some code here...
}
注意:如果你的控制器中包含一个名为 _remap() 的方法,那么不管你的 URI 中包含什么,它总会被忽略掉。这个方法会废除掉由 URI 片段来决定哪个方法被调用的规则,允许你重新定义调用方法的规则(方法的路由规则)。
既可以通过example.com/index.php/ blog / 来调用_remap()的方法,若_remap()有参数,则在/后加参数,调用具体的代码。
二、使用方法2例
但问题是,手册上这样说的,会有什么用处呢?其实用处有两个:
1、改变URL,隐藏方法,比如你的应用中,原来的URL方法是:
复制代码 代码如下:
example.com/index.php/blog/say
现在想改变显示的方法名为:
复制代码 代码如下:
example.com/index.php/blog/hello
但显示虽然是hello,但实际上是调用存在的say方法
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了,否则显示出错信息。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Notepad++7.3.1
Easy-to-use and free code editor

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6
Visual web development tools