Laravel5.1 启动详解
借鉴:
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当成黑盒来看,接收请求、处理请求。
稿源:七星互联Qixoo.com
Laravel5.1 启动详解的更多相关文章
- laravel5.1启动详解
laravel的启动过程 如果没有使用过类似Yii之类的框架,直接去看laravel,会有点一脸迷糊的感觉,起码我是这样的.laravel的启动过程,也是laravel的核心,对这个过程有一个了解,有 ...
- 深入浅出mybatis之启动详解
深入浅出mybatis之启动详解 MyBatis功能丰富,但使用起来非常简单明了,今天我们来追踪一下它的启动过程. 目录 如何启动MyBatis 如何使用MyBatis MyBatis启动过程 如何启 ...
- Linux 启动详解之init
1.init初探 init是Linux系统操作中不可缺少的程序之一.init进程,它是一个由内核启动的用户级进程,然后由它来启动后面的任务,包括多用户环境,网络等. 内核会在过去曾使用过init的几个 ...
- Linux启动详解
<概述> Linux启动大致分为一下几个步骤,详细的启动步骤在<启动分析>中详解. 1:首先bios加电自检,初始化(这个过程会检测相关硬件(cpu,内存,显卡,硬盘等)) 2 ...
- Linux开机启动详解
Linux开机启动程序详解 我们假设大家已经熟悉其它操作系统的引导过程,了解硬件的自检引导步骤,就只从Linux操作系统的引导加载程序(对个人电脑而言通常是LILO)开始,介绍Linux开机引导的步骤 ...
- Hadoop- 集群启动详解
NameNode启动过程详解 第一次启动:HDFS格式化后,生成fsimage文件 hdf
- Mongodb安装启动详解
最近在倒腾node+mongodb,安装mongodb的时候开始遇到很多问题,然后折腾了好几次,直到可以很顺利完成安装 ,所以把安装的过程记录下来. 线上系统基本上都是linux的,所以只安装了lin ...
- spring boot容器启动详解
目录 一.前言 二.容器启动 三.总结 =======正文分割线====== 一.前言 spring cloud大行其道的当下,如果不了解基本原理那么是很纠结的(看见的都是约定大于配置,但是原理呢?为 ...
- Springboot 启动详解
1.前言 最近一直在看Springboot和springcloud代码,看了将近20多天,对这两个系统的认知总算是入了门.后续应该会有一个系列的文章,本文就先从Springboot的启动入手. 2.容 ...
随机推荐
- mybatis order by绑定的参数
<select id = "queryByStartWithOrder" resultType="org.seckill.entity.SuccessKilled& ...
- jQuery的无new实例化
我只能说想法很好,设计的巧妙.看代码: var jQuery = function( selector, context ) { //执行了init函数并返回jQuery实例 return new j ...
- 24Spring_事务管理机制
第一部分:Spring事务管理高层抽象接口 我们介绍三个接口:1.PlatformTransactionManager 2.TransactionDefinition 3.TransactionSt ...
- Eclipse添加注释简介
(1)在方法或者属性上面添加注释:在方法或者属性字段的上面一行输/**,然后回车.一般情况下添加的注释格式如下所示,当然注释的格式是可以修改的: 1 2 3 4 5 /** * @param ...
- C语言 三级指针的应用
//三级指针的使用 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #includ ...
- C语言 指针与字符串
C语言可以在栈区 or 堆区 or 全局区 存放字符串,字符串不单单是存储在全局区的. //字符串与指针 #include<stdio.h> #include<stdlib.h> ...
- IPAdr.exe破解[练手]
[文章标题]: IPAdr.exe破解[软件名称]: IPAdr.exe[加壳方式]: 无[编写语言]: delphi[使用工具]: OD[作者声明]: 失误之处敬请诸位大侠赐教!---------- ...
- Sql复习之安全性与权限管理+vmware增加硬盘容量
参考资料: http://www.cnblogs.com/Jackeyzhang/archive/2011/05/18/2049621.html VmWare虚拟机增加硬盘容量的方法 http://b ...
- Git基础 - git blame
当想知道一段代码历史上有哪些人修改时,可以使用git blame查看,正如其名,当你看到那段让你抓狂的代码时,一定想找出是谁写的来一顿blame吧 : ) 使用方法 icebug@localhost: ...
- g++编译总结
g++编译&&gdb调试&&coredump调试 一.编译注意细节 1.使用g++编译CPP文件如果用gcc编译C++源文件时,加以下选项:-lstdc++,否则使用了 ...