zoukankan      html  css  js  c++  java
  • **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即可

  • 相关阅读:
    JAVA学习日记1-0706
    同步一个fork
    面试题 17.13. 恢复空格-7月9日
    3. 无重复字符的最长子串(leetcode)-7月8日
    面试题 16.11. 跳水板(leetcode)-7月8日
    112.路径总和(leetcode)-7月7日
    Git使用入门
    第一次尝试
    OpenPCDet: Open-MMLab 面向LiDAR点云表征的3D目标检测代码库
    人工智能和机器学习能为抗击新冠肺炎做些什么?
  • 原文地址:https://www.cnblogs.com/kenshinobiy/p/4415081.html
Copyright © 2011-2022 走看看