首页 > 其他分享 >041.CI4框架CodeIgniter,控制器过滤器Filter的使用

041.CI4框架CodeIgniter,控制器过滤器Filter的使用

时间:2024-09-01 21:36:43浏览次数:10  
标签:filter CodeIgniter after Filter filters CI4 array class before

01、我们在Filters目录,创建一个MyFilter.php文件

<?php

namespace App\Filters;

use CodeIgniter\Filters\FilterInterface;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;

class MyFilter implements FilterInterface
{
    public function before(RequestInterface $request, $arguments = null)
    {
        echo '我是Filter中的一句话。';
        //加上这一句,就不往下执行了
        //exit;
    }

    public function after(RequestInterface $request, ResponseInterface $response, $arguments = null)
    {
        // Do something here
    }
}

02、在app/Config/Filters.php目录,加上

 03、代码如下:

<?php

namespace Config;

use CodeIgniter\Config\Filters as BaseFilters;
use CodeIgniter\Filters\Cors;
use CodeIgniter\Filters\CSRF;
use CodeIgniter\Filters\DebugToolbar;
use CodeIgniter\Filters\ForceHTTPS;
use CodeIgniter\Filters\Honeypot;
use CodeIgniter\Filters\InvalidChars;
use CodeIgniter\Filters\PageCache;
use CodeIgniter\Filters\PerformanceMetrics;
use CodeIgniter\Filters\SecureHeaders;

class Filters extends BaseFilters
{
    /**
     * Configures aliases for Filter classes to
     * make reading things nicer and simpler.
     *
     * @var array<string, class-string|list<class-string>>
     *
     * [filter_name => classname]
     * or [filter_name => [classname1, classname2, ...]]
     */
    public array $aliases = [
        // 注册的过滤器
        'myfilter' => \App\Filters\MyFilter::class,
        'csrf' => CSRF::class,
        'toolbar' => DebugToolbar::class,
        'honeypot' => Honeypot::class,
        'invalidchars' => InvalidChars::class,
        'secureheaders' => SecureHeaders::class,
        'cors' => Cors::class,
        'forcehttps' => ForceHTTPS::class,
        'pagecache' => PageCache::class,
        'performance' => PerformanceMetrics::class,
    ];

    /**
     * List of special required filters.
     *
     * The filters listed here are special. They are applied before and after
     * other kinds of filters, and always applied even if a route does not exist.
     *
     * Filters set by default provide framework functionality. If removed,
     * those functions will no longer work.
     *
     * @see https://codeigniter.com/user_guide/incoming/filters.html#provided-filters
     *
     * @var array{before: list<string>, after: list<string>}
     */
    public array $required = [
        'before' => [
            'forcehttps', // Force Global Secure Requests
            'pagecache',  // Web Page Caching
        ],
        'after' => [
            'pagecache',   // Web Page Caching
            'performance', // Performance Metrics
            'toolbar',     // Debug Toolbar
        ],
    ];

    /**
     * List of filter aliases that are always
     * applied before and after every request.
     *
     * @var array<string, array<string, array<string, string>>>|array<string, list<string>>
     */
    public array $globals = [
        'before' => [
            // 'auth' => ['except' => ['login', 'logout']],
            // 'honeypot',
            // 'csrf',
            // 'invalidchars',
        ],
        'after' => [
            // 'honeypot',
            // 'secureheaders',
        ],
    ];

    /**
     * List of filter aliases that works on a
     * particular HTTP method (GET, POST, etc.).
     *
     * Example:
     * 'POST' => ['foo', 'bar']
     *
     * If you use this, you should disable auto-routing because auto-routing
     * permits any HTTP method to access a controller. Accessing the controller
     * with a method you don't expect could bypass the filter.
     *
     * @var array<string, list<string>>
     */
    public array $methods = [];

    /**
     * List of filter aliases that should run on any
     * before or after URI patterns.
     *
     * Example:
     * 'isLoggedIn' => ['before' => ['account/*', 'profiles/*']]
     *
     * @var array<string, array<string, list<string>>>
     */
    public array $filters = [
        'myfilter' => ['before' => ['/', 'api/v1/users/user/*']],
    ];
}

04、我们在任意一个控制器中,增加一个函数

05、 效果如下,浏览器可以看到执行前内容了。

 

标签:filter,CodeIgniter,after,Filter,filters,CI4,array,class,before
From: https://www.cnblogs.com/tianpan2019/p/18391777

相关文章

  • Filter管道
    usingCronos;usingNewtonsoft.Json;usingSystem.Collections;usingSystem.Collections.Concurrent;usingSystem.Collections.Generic;usingSystem.Linq.Expressions;usingSystem.Reflection;usingSystem.Threading;namespaceConsoleApp1{internalcla......
  • 040.CI4框架CodeIgniter,使用Response实现前后端分离并返回JSON
    01、BaseController.php代码如下:<?phpnamespaceApp\Controllers;useCodeIgniter\Controller;useCodeIgniter\HTTP\CLIRequest;useCodeIgniter\API\ResponseTrait;useCodeIgniter\HTTP\IncomingRequest;useCodeIgniter\HTTP\RequestInterface;useCo......
  • 039.CI4框架CodeIgniter,封装Model模型绑定数据库的封装
    01、ModelBase.php代码如下:<?phpnamespaceApp\Models;useCodeIgniter\Database\ConnectionInterface;useCodeIgniter\Model;useCodeIgniter\Validation\ValidationInterface;classModelBaseextendsModel{var$Db;function__construct(Conn......
  • 037.CI4框架CodeIgniter,使用Model模型绑定数据库表
    01、我们创建一个数据库,如下:CREATETABLE`user`(`id`int(20)NOTNULLAUTO_INCREMENT,`username`varchar(255)CHARACTERSETutf8COLLATEutf8_general_ciNULLDEFAULTNULL,`userpassword`varchar(255)CHARACTERSETutf8COLLATEutf8_general_ciNULLDE......
  • Vue 过滤器(Filter)的理解与用法
    Vue.js是一个渐进式JavaScript框架,它提供了丰富的功能来构建用户界面。其中,过滤器(Filter)是一个非常有用的特性,它允许我们在模板中对数据进行格式化处理。本文将详细介绍Vue过滤器的概念、用法以及一些最佳实践。1.过滤器的基本概念1.1什么是过滤器?过滤器是Vue提供的一种......
  • 【Python进阶】Python中的函数式编程元素:map、filter和reduce的妙用
    1、函数式编程概览1.1函数式编程起源与发展函数式编程这一概念可以追溯到20世纪30年代的λ演算理论,这一时期数学家们开始探讨如何通过纯粹的函数运算来构建计算模型。随着时间的推移,函数式编程逐渐发展成为一种重要的编程范式,并在Lisp、Scheme、Haskell等语言中得到了充......
  • Cookie,Session Filter,Listener详解
    HTTP请求的无状态性HTTP的无状态性是其一个重要的特征,指的是HTTP协议本身并不保留客户端与服务器交互的历史信息,换而言之,即每次的HTTP请求都是独立的,服务器在处理每一个请求时都不会记住前一个请求的状态无状态的含义独立性:每次的HTTP请求都是独立的,不依赖于之前的请......
  • python map/filter/reduce的用法示例
    fromfunctoolsimportreducedeffunc0(a):"""a:可迭代对象的迭代元素将function应用于可迭代对象的对应元素,并返回一个迭代器,其中包含了所有映射后的结果map(function,iterable,...)function:要应用于可迭代对象的函数。iterable:要进行映射......
  • Java中stream流的filter机制理解
    一开始我并不理解,为什么filter接受一个实现了Predicate接口的对象后,就能通过重写的test方法来筛选元素,这个将筛选后的元素放到新的流中,我并不知道如何实现的。后来我发现ReferencePipeline抽象类中重写了filter方法,并且Collection中的stream方法返回的是StreamSupport.stream方法......
  • 过滤器Filter
    目录一、概述二、Filter接口1.源码2.API三、使用1.步骤2.需求3.代码 四、过滤器的生命周期五、过滤器链的使用六、注解方式配置过滤器一、概述Filter,即过滤器,是JAVAEE技术规范之一,作用目标资源的请求进行过滤的一套技术规范,是JavaWeb项目中最为实用的技......