中间件构造函数传参,中间件里如何获取参数

hackxiaoya
125 1 0
1个回答

walkor

就是普通类构造函数用法,比如传进去后存到类属性里

<?php
namespace app\middleware;

use Webman\MiddlewareInterface;
use Webman\Http\Response;
use Webman\Http\Request;

class MiddlewareA implements MiddlewareInterface
{

    protected $param1;

    protected $param2;

    public function __construct($param1, $param2)
    {
        $this->param1 = $param1;
        $this->param2 = $param2;
    }

    public function process(Request $request, callable $handler) : Response
    {
        var_dump($this->param1, $this->param2);
        return $handler($request);
    }

}
  • 暂无评论
🔝