**CodeIgniter系列 添加filter和helper
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的更多相关文章
- [Asp.net MVC]Asp.net MVC5系列——添加模型
目录 概述 添加模型 总结 系列文章 [Asp.net MVC]Asp.net MVC5系列——第一个项目 [Asp.net MVC]Asp.net MVC5系列——添加视图 概述 在本节中我们将追加 ...
- [Asp.net MVC]Asp.net MVC5系列——添加数据
目录 概述 显示添加数据时所用表单 处理HTTP-POST 总结 系列文章 [Asp.net MVC]Asp.net MVC5系列——第一个项目 [Asp.net MVC]Asp.net MVC5系列 ...
- Asp.net MVC3 中,动态添加filter
Asp.net MVC3 中,动态添加filter filter是attribute,不支持泛型,传入的参数必须是固定的值.总之很受attribute本身的限制. 发现一篇老外的文章,动态设置filt ...
- 通过添加filter过滤器 彻底解决ajax 跨域问题
1.在web.xml添加filter <filter> <filter-name>contextfilter</filter-name> <filter-cl ...
- Jetty添加Filter过滤器
1.Jetty嵌入到Spring项目 try { Server server = new Server(8080); WebAppContext context = new WebAppContext ...
- java项目中通过添加filter过滤器解决ajax跨域问题
1.在web.xml添加filter <filter> <filter-name>contextfilter</filter-name> <filter-cl ...
- Springboot添加filter方法
在springboot添加filter有两种方式: (1).通过创建FilterRegistrationBean的方式(建议使用此种方式,统一管理,且通过注解的方式若不是本地调试,如果在filter中 ...
- [Asp.net MVC]Asp.net MVC5系列——添加视图
目录 系列文章 概述 添加视图 总结 系列文章 [Asp.net MVC]Asp.net MVC5系列——第一个项目 概述 在这一部分我们添加一个新的控制器HelloWorldController类, ...
- cas sso单点登录系列1_cas-client Filter源码解码(转)
转:http://blog.csdn.net/ae6623/article/details/8841801?utm_source=tuicool&utm_medium=referral /* ...
随机推荐
- Mobile Service
link 试题分析 我们发现$dp(t,s1,s2,s3)$表示在$t$时刻$3$个人的位置.发现时间复杂度为$O(n \times L^3)$.不仅会$T$还会$MLE$,所以需要优化$dp$.我们 ...
- 2016-2017 ACM-ICPC East Central North America Regional Contest (ECNA 2016) F 区间dp
Problem F Removal GameBobby Roberts is totally bored in his algorithms class, so he’s developed a li ...
- mybatis中的resultMap与resultType、parameterMap与 parameterType的区别
Map:映射:Type:Java类型 resultMap 与 resultType.parameterMap 与 parameterType的区别在面试的时候被问到的几率非常高,项目中出现了一个小b ...
- Linux中怎么终止正在运行的后台程序
linux 任务管理-后台运行与终止fg.bg.jobs.&.ctrl + z命令一. &加在一个命令的最后,可以把这个命令放到后台执行 ,如gftp &,二.ctrl + z ...
- count distinct
SELECT COUNT(DISTINCT Customer) AS NumberOfCustomers FROM Orders
- SQL Server 2008过期导致MSSQLSERVER服务无法启动现象
SQL Server 2008过期导致MSSQLSERVER服务无法启动现象:安装的是SQL Server 2008评估版,180天的试用期后,MSSQLSERVER服务就无法启动,手动启动就报告17 ...
- 笔记(二)TabLayout + ViewPager + FragmentPagerAdapter 组合用法
TabLayout的xml文件 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ...
- spring boot(二):注解大全
spring boot注解 @Autowired 注解的意思就是,当Spring发现@Autowired注解时,将自动在代码上下文中找到和其匹配(默认是类型匹配)的Bean,并自动注入到相应的地方去. ...
- .NET面试题系列(二)GC
序言 对象生存期 Phone item=new Phone() 在C#中,创建对象使用的是new关键字. 要注意的是new操作返回的并不是对象本身,而是对象的一个引用(Reference). 如果使用 ...
- [AHOI2012]树屋阶梯 题解(卡特兰数)
[AHOI2012]树屋阶梯 Description 暑假期间,小龙报名了一个模拟野外生存作战训练班来锻炼体魄,训练的第一个晚上,教官就给他们出了个难题.由于地上露营湿气重,必须选择在高处的树屋露营. ...