为什么tp5.1的middleware必须要有handle方法

博客围绕ThinkPHP框架中thinkphp\\libary\\think\\Middleware里的add()方法展开,同时探讨了该文件中$requst和next存在的原因,聚焦于ThinkPHP框架的相关技术细节。

1,在thinkphp\libary\think\Middleware中的add()方法有关

  /**
     * 注册中间件
     * @access public
     * @param  mixed  $middleware
     * @param  string $type  中间件类型
     */
    public function add($middleware, $type = 'route')
    {
        if (is_null($middleware)) {
            return;
        }

        $middleware = $this->buildMiddleware($middleware, $type);

        if ($middleware) {
            $this->queue[$type][] = $middleware;
        }
    }

 /**
     * 解析中间件
     * @access protected
     * @param  mixed  $middleware
     * @param  string $type  中间件类型
     */
    protected function buildMiddleware($middleware, $type = 'route')
    {
        if (is_array($middleware)) {
            list($middleware, $param) = $middleware;
        }

        if ($middleware instanceof \Closure) {
            return [$middleware, isset($param) ? $param : null];
        }

        if (!is_string($middleware)) {
            throw new InvalidArgumentException('The middleware is invalid');
        }

        if (false === strpos($middleware, '\\')) {
            if (isset($this->config[$middleware])) {
                $middleware = $this->config[$middleware];
            } else {
                $middleware = $this->config['default_namespace'] . $middleware;
            }
        }

        if (is_array($middleware)) {
            return $this->import($middleware, $type);
        }

        if (strpos($middleware, ':')) {
            list($middleware, $param) = explode(':', $middleware, 2);
        }

        return [[$this->app->make($middleware), 'handle'], isset($param) ? $param : null];
    }

2,为何要有$requst,和next(也是在此文件中)

 /**此方法在app的run()中执行
     * 中间件调度
     * @access public
     * @param  Request  $request
     * @param  string   $type  中间件类型
     */
    public function dispatch(Request $request, $type = 'route')
    {
        return call_user_func($this->resolve($type), $request);
    }

    // 被多次调用因为里面的 $middleware = array_shift($this->queue[$type]); call_user_func_array($call, [$request, $this->resolve($type), 
    protected function resolve($type = 'route')
    {
        return function (Request $request) use ($type) {

            $middleware = array_shift($this->queue[$type]);
            if (null === $middleware) {
                throw new InvalidArgumentException('The queue was exhausted, with no response returned');
            }

            list($call, $param) = $middleware;

            try {
                $response = call_user_func_array($call, [$request, $this->resolve($type), $param]);//要传入的参数是$request $next
            } catch (HttpResponseException $exception) {
                $response = $exception->getResponse();
            }

            if (!$response instanceof Response) {
                throw new LogicException('The middleware must return Response instance');
            }

            return $response;
        };
    }

 

在ThinkPHP 5.1(简称TP5.1)中,设置全局变量可以通过多种方式实现。以下是几种常见的方法: ### 1. 使用配置文件 你可以在`config`目录下创建或编辑配置文件来设置全局变量。例如,编辑`config.php`文件: ```php <?php return [ 'app_debug' => true, 'app_trace' => true, 'global_var' => 'This is a global variable', ]; ``` 在控制器或其他地方使用全局变量: ```php $globalVar = config('global_var'); ``` ### 2. 使用`hook`或中间件 你可以在中间件中设置全局变量。例如,创建一个中间件`app/middleware/GlobalVar.php`: ```php <?php namespace app\middleware; class GlobalVar { public function handle($request, \Closure $next) { $request->globalVar = 'This is a global variable'; return $next($request); } } ``` 在`config/middleware.php`中注册中间件: ```php return [ 'alias' => [ 'global_var' => \app\middleware\GlobalVar::class, ], 'global_var' => [ 'global_var', ], ]; ``` 在控制器中使用: ```php $globalVar = $this->request->globalVar; ``` ### 3. 使用服务提供者 你可以在服务提供者中设置全局变量。例如,创建一个服务提供者`app/provider/GlobalVarServiceProvider.php`: ```php <?php namespace app\provider; use think\Service; class GlobalVarServiceProvider extends Service { public function register() { $this->app->bind('global_var', function () { return 'This is a global variable'; }); } } ``` 在`config/provider.php`中注册服务提供者: ```php return [ 'aliases' => [ // ... ], 'providers' => [ // ... \app\provider\GlobalVarServiceProvider::class, ], ]; ``` 在控制器中使用: ```php $globalVar = $this->app->global_var; ``` ### 4. 使用环境变量 你可以在`.env`文件中设置全局变量: ```env GLOBAL_VAR=This is a global variable ``` 在控制器或其他地方使用: ```php $globalVar = env('GLOBAL_VAR'); ``` 通过以上方法,你可以在TP5.1中设置和使用全局变量。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值