在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了,否则显示出错信息
摘自 jackyrong

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

SublimeText3 Mac version
God-level code editing software (SublimeText3)

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version
Chinese version, very easy to use

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