**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 /* ... 
随机推荐
- 流媒体协议之RTP详解20170921
			1.RTP介绍 实时传输协议RTP(Real-time Transport Protocol)是一个网络传输协议,它是由IETF的多媒体传输工作小组1996年在RFC 1889中公布的,后在RFC35 ... 
- Overlaying GPS Coordinates for Camera Crosshairs
			Hey Guys! I am working on a project to allow us to implement GPS coordinates for the location of the ... 
- Codeforces Round #340 (Div. 2) E 莫队+前缀异或和
			E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megabytes input s ... 
- 我的emacs简易配置
			;;------------语言环境字符集设置(utf-8)------------- (set-language-environment 'Chinese-GB) (set-keyboard-cod ... 
- VS Code 配置 C/C++ 环境
			写作原因 微软的 VSCode 一直以来为人诟病的一个问题就是对于 C/C++ 工程的编译以及调试支持度有限,配置起来比较复杂,但是 vscode-cpptools 团队经过一段时间的 bug 修复之 ... 
- 在GitHub搭建个人博客 地址: https://douzujun.github.io/
			搭建博客地址:https://douzujun.github.io/ 博客模板:https://github.com/douzujun/douzujun.github.io 显示效果: 
- 庞老师集群.ziw
			2017年2月17日, 星期五 庞老师集群 链接:http://pan.baidu.com/s/1mhSw2TE 密码:hzz4 更改子网IP,及网关: null 
- IIS 网站日志分析
			最近由于ADSL代理总出问题,导致爬虫服务器总被目标网站封,由于请求内容总是空,前端APP获取不到想要的内容就一直刷新,导致爬虫服务器请求更加繁忙. 爬虫服务器每执行完一个流程,都会给统计服务器Pos ... 
- h5 canvas动画,不知道谁写的
			<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ... 
- docker 镜像导入和导出
			使用 docker commit 即可把这个容器变为一个镜像 docker commit 8d93082a9ce1 ubuntu:myubuntu 这时候 docker 容器会被创建为一个新的 Ubu ... 
