font-size: 14px;
background-color: rgb(247, 252, 255);
">CI 的钩子功能使得您能够在不改动系统核心文件的基础上来改变或添加系统的核心执行功能。</span></p><p><span style="font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 14px; background-color: rgb(247, 252, 255); ">
比如,您能够在控制器刚刚加载前或刚刚加载后来执行特定的脚本,或者在其它时刻来触发您的脚本。
</span></p><p><span style="font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 14px;
background-color: rgb(247, 252, 255);
">看代码:</span></p><pre name="code" class="php"><span style="background-color: rgb(247, 252, 255); font-family: Verdana, Arial, Helvetica, sans-serif; ">
</span>
system/application/config/hooks.php中加入钩子声明:
[php]
$hook['post_controller_constructor'] = array(
'class' => 'Acl',
'function' => 'filter',
'filename' => 'acl.php',
'filepath' => 'hooks',
);
system/application/config/config.php中让钩子系统生效
$config['enable_hooks'] = TRUE;
然后在中新建acl.php权限系统配置文件,当然你也能够放在数据库中。
//游客权限映射
$config['acl']['visitor'] = array(
'' => array('index'),//首页 www.2cto.com
'music' => array('index', 'list'),
'user' => array('index', 'login', 'register')
);
//管理员
$config['acl']['admin'] = array(
);
//-------------配置权限不够的提示信息及跳转url------------------//
$config['acl_info']['visitor'] = array(
'info' => '须要登录以继续',
'return_url' => 'user/login'
);
$config['acl_info']['more_role'] = array(
'info' => '须要更高权限以继续',
'return_url' => 'user/up'
);
/* End of file acl.php */
/* Location: ./application/config/acl.php */
system/application/hooks文件夹下加入acl.php逻辑处理文件
class Acl
{
private $url_model;//所訪问的模块,如:music
private $url_method;//所訪问的方法,如:create
private $url_param;//url所带參数 可能是 1 也可能是 id=1&name=test
private $CI;
function Acl()
{
$this->CI = & get_instance();
$this->CI->load->library('session');
$url = $_SERVER['PHP_SELF'];
$arr = explode('/', $url);
$arr = array_slice($arr, array_search('index.php', $arr) + 1, count($arr));
$this->url_model = isset($arr[0]) ? $arr[0] : '';
$this->url_method = isset($arr[1]) ? $arr[1] : 'index';
$this->url_param = isset($arr[2]) ? $arr[2] : '';
}
function filter()
{
$user = $this->CI->session->userdata('user');
if (emptyempty($user)) {//游客visitor
$role_name = 'visitor';
} else {
$role_name = $user->role;
}
$this->CI->load->config('acl');
$acl = $this->CI->config->item('acl');
$role = $acl[$role_name];
$acl_info = $this->CI->config->item('acl_info');
if (array_key_exists($this->url_model, $role) && in_array($this->url_method, $role[$this->url_model])) {
;
} else {//无权限,给出提示,跳转url
$this->CI->session->set_flashdata('info', $acl_info[$role_name]['info']);
redirect($acl_info[$role_name]['return_url']);
}
}
}