【laravel-tutorial】

请求生命周期

介绍

当你对于自己使用的框架运行原理有更加的熟悉,你用起来会更加的自信。这个部分主要较少laravel框架是如何运作,以及原理,如果有些概念看起来比较陌生,会在接下来的部分做更详细的介绍

总览

第一步

  1. 入口文件 public/index.php
  2. 自动加载,获取实例
  3. 创建 service container的实例
    index.php的核心代码如下
$app = require_once __DIR__.'/../bootstrap/app.php';

$kernel = $app->make(Kernel::class);

$response = $kernel->handle(
    $request = Request::capture()
)->send();

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

HTTP/Console 内核

1、bootstrappers 中包含了所有在请求开始之前需要加载的内容

protected $bootstrappers = [
        \Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables::class,
        \Illuminate\Foundation\Bootstrap\LoadConfiguration::class,
        \Illuminate\Foundation\Bootstrap\HandleExceptions::class,
        \Illuminate\Foundation\Bootstrap\RegisterFacades::class,
        \Illuminate\Foundation\Bootstrap\RegisterProviders::class,
        \Illuminate\Foundation\Bootstrap\BootProviders::class,
    ];

2、看到 HTTP下的Kernel类继承了 Illuminate\Foundation\Http\Kernel
内部的handler如下

public function handle($request)
    {
        try {
            $request->enableHttpMethodParameterOverride();

            $response = $this->sendRequestThroughRouter($request);
        } catch (Throwable $e) {
            $this->reportException($e);

            $response = $this->renderException($request, $e);
        }

        $this->app['events']->dispatch(
            new RequestHandled($request, $response)
        );

        return $response;
    }

可以看出是很简单,一个请求、请求处理、返回。内部为黑盒

Service Providers

在 coinfig/app.php下有一个数组,包含了所有的服务提供者

'providers' => [

        /*
         * Laravel Framework Service Providers...
         */
        Illuminate\Auth\AuthServiceProvider::class,
        Illuminate\Broadcasting\BroadcastServiceProvider::class,
        Illuminate\Bus\BusServiceProvider::class,
        Illuminate\Cache\CacheServiceProvider::class,
        Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class,
        Illuminate\Cookie\CookieServiceProvider::class,
        Illuminate\Database\DatabaseServiceProvider::class,
        Illuminate\Encryption\EncryptionServiceProvider::class,
        Illuminate\Filesystem\FilesystemServiceProvider::class,
        Illuminate\Foundation\Providers\FoundationServiceProvider::class,
        Illuminate\Hashing\HashServiceProvider::class,
        Illuminate\Mail\MailServiceProvider::class,
        Illuminate\Notifications\NotificationServiceProvider::class,
        Illuminate\Pagination\PaginationServiceProvider::class,
        Illuminate\Pipeline\PipelineServiceProvider::class,
        Illuminate\Queue\QueueServiceProvider::class,
        Illuminate\Redis\RedisServiceProvider::class,
        Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
        Illuminate\Session\SessionServiceProvider::class,
        Illuminate\Translation\TranslationServiceProvider::class,
        Illuminate\Validation\ValidationServiceProvider::class,
        Illuminate\View\ViewServiceProvider::class,

        /*
         * Package Service Providers...
         */

        /*
         * Application Service Providers...
         */
        App\Providers\AppServiceProvider::class,
        App\Providers\AuthServiceProvider::class,
        // App\Providers\BroadcastServiceProvider::class,
        App\Providers\EventServiceProvider::class,
        App\Providers\RouteServiceProvider::class,
        Mews\Captcha\CaptchaServiceProvider::class,
    ],

laravel会自动遍历实例化每一个服务提供者。在完成实例化之后,会调用服务提供者内部的register方法。在所有的register调用完毕后,执行内部的boot方法

路由

1、所有的请求都是通过路由分发到对应controller的方法当中,在方法中处理数据,然后返回数据(可以是页面,也可以是具体的数据如json或者xml)
2、加载原理是 RouteServiceProvider 这个服务提供者
3、结果中间件处理。有自动加载的中间件,比如内核中加载的,也有自己定制的中间件。可以详细看中间件的部分。

聚焦在服务提供者

  1. 是laravel的核心
  2. 主要存放在app/Provider目录下
  3. AppServiceProvider 提供给用户自动使用
posted @   爱喝可乐的小企鹅  阅读(27)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
点击右上角即可分享
微信分享提示