中间件的使用

    系统级中间件可以在Config夹下新建

    \Config\Middleware.php

    1. return [
    2. // \SilangPHP\Middleware\SystemtestMiddleware::class
    3. ];


    1. <?php
    2. namespace App\Controller;
    3. use App\Middleware\HelloMiddleware;
    4. use App\Middleware\TestMiddleware;
    5. class IndexController extends \SilangPHP\Controller
    6. {
    7. // 要运行的中间件
    8. public $middlewares = [HelloMiddleware::class,TestMiddleware::class];
    9. // 只能运行的Action
    10. public $onlyAction = [];
    11. // 不能运行的Action
    12. public $exceptAction = [];
    13. // onlyAction和exceptAction不同即所有Action都使用中间件
    14. public function index(\SilangPHP\Request $request)
    15. {
    16. echo 'action内容:index'.lr;
    17. }
    18. public function index2()
    19. {
    20. echo 'index3';
    21. }
    22. }

    输出

    中间件test start
    中间件hello start
    action内容:index
    中间件hello end
    中间件test end