GithubHelp home page GithubHelp logo

codeigniter-middleware's Introduction

codeigniter-middleware

CI2框架中间件实现

1. 在自己的控制器基类中继承中间件Controller

class MY_Controller extends Codeigniter\Middleware\CM_Controller
{

}

2. 在框架应用目录的core下创建自己的[subclass_prefix]_Loader.php 并继承中间件Loader

class MY_Loader extends Codeigniter\Middleware\CM_Loader
{

}

3. 现在你可以在应用下的middlewares目录下创建自己的中间件啦

class throttle_middleware extends Codeigniter\Middleware\Abstracts\CI_Middleware
{
    public function handle(\Closure $next, $maxAttempts = 60, $decayMinutes = 1)
    {
        return $next();
    }
}
class Test extends MY_Controller
{
    public function init()
    {
        // 中间件
        $this->middleware('csrf_middleware');

        // 带参数的中间件
        $this->middleware('throttle_middleware', [
            'params' => [60,10],
            'except' => ['index']
        ]);

        // 匿名中间件
        $this->middleware(function($next){
            if(true){
                return ['error_code' => 1, 'error_msg' => '我拦住你了'];
            }
            return $next();
        }, [
            'only' => ['index']
        ]);

        // 带后置操作的匿名中间件
        $this->middleware(function($next){
            echo 'this is test route.';
            $response = $next();
            echo "后置中间件";
            return $response;
        }, [
            'except' => ['index'],
        ]);
        
    }

    public function index()
    {
        echo "hello world";
    }

    public function test1()
    {
        echo "test1";
    }

    public function test2()
    {
        echo "test2";
    }

    public function notify()
    {
        echo date('Y-m-d H:i:s');
    }
    
}

codeigniter-middleware's People

Contributors

ericzhao007 avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.