Skip to content

终止程序

php
$kernel->terminate($request, $response);

该方法是Kernel类的方法

terminate()方法

php
public function terminate($request, $response)
{
    $this->terminateMiddleware($request, $response);
    // $this->app是指向Application类实例的,所以查看Application类的terminate方法
    $this->app->terminate();
}
/**
 * 调用所有中间件的terminate方法 
 */
protected function terminateMiddleware($request, $response)
{
    $middlewares = $this->app->shouldSkipMiddleware() ? [] : array_merge(
        $this->gatherRouteMiddleware($request),
        $this->middleware
    );

    foreach ($middlewares as $middleware) {
        if (! is_string($middleware)) {
            continue;
        }

        [$name] = $this->parseMiddleware($middleware);

        $instance = $this->app->make($name);

        if (method_exists($instance, 'terminate')) {
            $instance->terminate($request, $response);
        }
    }
}

public function terminate()
{
    $index = 0;

    while ($index < count($this->terminatingCallbacks)) {
        $this->call($this->terminatingCallbacks[$index]);

        $index++;
    }
}

整个流程就是调用所有中间件的terminate方法,然后调用Application类的terminate方法