Laravel5.1 启动详解
借鉴:http://www.cnblogs.com/wish123/p/4756669.html
Laravel所有请求的入口文件是:/public/index.php,代码如下
<?php /* |-------------------------------------------------------------------------- | Register The Auto Loader |-------------------------------------------------------------------------- | | Composer provides a convenient, automatically generated class loader for | our application. We just need to utilize it! We'll simply require it | into the script here so that we don't have to worry about manual | loading any of our classes later on. It feels nice to relax. | */
//自动加载文件设置 require __DIR__.'/../bootstrap/autoload.php'; /* |-------------------------------------------------------------------------- | Turn On The Lights |-------------------------------------------------------------------------- | | We need to illuminate PHP development, so let us turn on the lights. | This bootstraps the framework and gets it ready for use, then it | will load up this application so that we can run it and send | the responses back to the browser and delight our users. | */
//初始化服务容器,即框架的 IoC 容器,生成一个Laravel应用实例 $app = require_once __DIR__.'/../bootstrap/app.php'; /* |-------------------------------------------------------------------------- | Run The Application |-------------------------------------------------------------------------- | | Once we have the application, we can handle the incoming request | through the kernel, and send the associated response back to | the client's browser allowing them to enjoy the creative | and wonderful application we have prepared for them. | */
//通过服务容器生成一个 HTTP / Console kernel类的实例 $kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
//kernel类实例运行handle方法,接收请求和处理结果(主要是运行middleware和URL相关的controller) $response = $kernel->handle( $request = Illuminate\Http\Request::capture() );
//返回处理结果 $response->send();
//为整个请求处理周期展示最后的任何想要提供的动作,如每次访问后想要记录用户行为、给前端发消息 $kernel->terminate($request, $response);
(1)自动加载文件
自动加载文件功能是通过Composer实现的,在composer.json中设置的加载方式里提取加载路径,然后根据Laravel对应的加载方式进行处理,主要为四种加载方式:
- PSR0加载方式—对应的文件就是autoload_namespaces.php
- PSR4加载方式—对应的文件就是autoload_psr4.php
- 其他加载类的方式—对应的文件就是autoload_classmap.php
- 加载公用方法—对应的文件就是autoload_files.php
具体的定义形式在 composer.json 里面设置路径,如下:
"autoload": { "classmap": [ "database" ], "psr-4": { "App\\": "app/", "App\\Http\\Models\\": "app/Http/Models/" } }, "autoload-dev": { "classmap": [ "tests/TestCase.php" ] },
(2)初始化服务容器
The Laravel service container is a powerful tool for managing class dependencies and performing dependency injection.
这句话是指 Laravel 服务容器的核心是依赖注入(也是控制反转,一种设计模式),它是把在类里面定义实例的这种高耦合模式摒弃掉,虽然工厂模式也行,不过也比较不灵活,然后以一种依赖的方式去把相关的类注册在容器里,需要时再把类实例从容器提取出来注入到本身。
本质上是分离,将各种功能分离开,保证了松耦合,例如应用程序用到Foo类,Foo类需要Bar类,Bar类需要Bim类,那么先创建Bim类,再创建Bar类并把Bim注入,再创建Foo类,并把Bar类注入,再调用Foo方法,Foo调用Bar方法,接着做些其它工作。
Laravel这个过程粗略的有以下几个步骤:
1、registerBaseBindings:初始化基本的容器实例。
2、registerBaseServiceProviders:注册基本的service provider,有两个:event、route。event service provider会设置队列的处理工厂,route service provider定义一些路由行为,包括请求、反应、重定向这些。
3、registerCoreContainerAliases:注册核心的容器功能类的别名,这些类包括验证、缓存、配置、队列、session等。
(3)生成kernel类
The HTTP kernel extends the Illuminate\Foundation\Http\Kernel
class, which defines an array of bootstrappers
that will be run before the request is executed. These bootstrappers configure error handling, configure logging, detect the application environment, and perform other tasks that need to be done before the request is actually handled.
The HTTP kernel also defines a list of HTTP middleware that all requests must pass through before being handled by the application. These middleware handle reading and writing the HTTP session, determine if the application is in maintenance mode, verifying the CSRF token, and more.
(4)kernel handle处理
将kernel当成黑盒来看,接收请求、处理请求。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现