CodeIgniter系列 添加filter和helper
来源:互联网 发布:图片文字修改软件 编辑:程序博客网 时间:2024/05/21 17:20
filter:
使用CI的hooks来实现filter.
1.在system/application/config/config.php中,把enable_hooks的值改为TRUE
$config['enable_hooks'] = TRUE;
2.在syste/application/config/hooks.php中,添加hooks,如下
$hook['post_controller_constructor'] = array(
'class' => 'SecurityFilterChain',
'function' => 'do_filter',
'filename' => 'security_filter_chain.php',
'filepath' => 'hooks',
'params' => array(
'logged_in_session_attr' => 'logged_in',
'login_page' => '/login/',
'should_not_filter' => array('/^//login$/', '/^//login//.*$/', '/^//user//profile.*$/'),
'need_admin_role' => array('/^//user$/', '/^//user//.*$/', '/^//role$/', '/^//role//.*$/')
)
);
其中params 是传递给filter类的参数.
shoud_not_filter是不需要过滤的uri
need_admin_role是需要管理员角色的uri
3.生成文件system/application/hooks/security_filter_chain.php
class SecurityFilterChain {
function do_filter($params)
{
$CI = &get_instance();
$uri = uri_string();
foreach($params['should_not_filter'] as $not_filter)
{
if(preg_match($not_filter, $uri) == 1)
{
return;
}
}
if(!$CI->session->userdata($params['logged_in_session_attr']))
{
redirect($params['login_page']);
}
foreach($params['need_admin_role'] as $need_admin)
{
if(preg_match($need_admin, $uri) == 1)
{
$current_user = $CI->session->userdata('current_user');
if(!isset($current_user['role_status']) or $current_user['role_status'] != 0) // 0表示管理员角色的id
{
show_error('您没有权限访问这个页面', 403);
return;
}
break;
}
}
}
}
helper
添加自定义的helper,名称为test
1.创建文件system/application/helpers/test_helper.php内容为:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
if ( ! function_exists('array_to_option'))
{
function array_to_option($name, $data = array())
{
$html = "<select name=/"$name/">";
foreach($data as $value => $text)
{
$html .= "<option value=/"$value/">$text</option>";
}
$html .= "</select>";
return $html;
}
}
2.加载这个helper
在autoload.php里边,autoload['helper']中添加test
$autoload['helper'] = array('url', 'form', 'test');
或者在controller的构造函数中添加
$this->load->helper('test')
3.使用。直接调用函数array_to_option即可
- CodeIgniter系列 添加filter和helper
- CodeIgniter系列 添加filter和helper
- CodeIgniter Dojo Helper
- CKEditor Helper for CodeIgniter
- Codeigniter Helper辅助函数
- CodeIgniter系列 记录count和分页
- 在codeigniter的helper用phpmailer 发送邮件
- nodejs handlebars 如何添加helper
- codeigniter和mvc
- codeigniter和mvc续
- Spring MVC根据运行参数添加filter代理(启动时动态添加filter和listener)
- Helper
- Chorme浏览器添加XPath Helper插件
- helper和plugin的区别
- SpringBoot系列<八>过滤器filter和监听器listener配置
- CodeIgniter框架添加service层增强可复用性
- 《PHP框架CodeIgniter》系列技术文章整理收藏
- 【Lucene3.6.2入门系列】第07节_高级搜索之普通Filter和自定义Filter
- C++中的vector使用范例
- 工信部备案网站打不开的临时解决办法
- Android 2.2系统比2.1版性能高450%
- replica handbags
- How to detect "edit mode" in excel
- CodeIgniter系列 添加filter和helper
- 关于myeclipse新建jsp时pageEncoding的设置
- 10-05-13
- 雅虎杨致远低调来京 曾拜会微软、创新工厂
- FFMpeg框架代码阅读
- rpm specification file
- request.getContextPath()的用法
- 1080P和720P电影的分辨率到底是多少
- 健身计划