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.容 ...
随机推荐
- String类常用方法。
一,字符数组与字符串. 一个字符串可以变成一个字符数组,同样,一个字符数组可以变成一个字符串. 在String类中提供了以下操作方法. 1)将字符串变成字符数组:public char[] toCha ...
- $watch方法
监听一个model(表单),当一个model每次改变时,都会触发第二个参数函数 $scope.$watch('name',function(){});//name是model名<input ty ...
- struts2中简单的文件上传
2016-08-31 一. 文件上传 利用commons-fileupload-1.2.1.jar实现简单的上传文件,首先在页面上填写表单,记得加上enctype="multip ...
- android 中打 Log 的一些技巧
在 android 平台上搞开发工作,会经常用到一些 Log 输出调试信息. 众所周知,android 中有五种类型的 Log , v, d, i, w, e 这里就不再赘 述 (如果对这些不了解的朋 ...
- OAF与XML Publisher集成(转)
原文地址:OAF与XML Publisher集成 有两种方式,一种是用VO与XML Publisher集成,另一种是用PL/SQL与XML Publisher集成 用VO与XML Publisher集 ...
- JQuery判断数组中是否包含某个元素$.inArray("js", arr);
var arr = [ "xml", "html", "css", "js" ]; $.inArray(" ...
- 信息安全系统设计基础实验一:Linux开发环境的配置和使用
北京电子科技学院(BESTI) 实验报告 课程:信息安全系统设计基础 班级:1353 姓名:芦畅 傅冬菁 学号:20135308 20135311 成绩: 指导教师:娄家鹏 ...
- 交流异步电机的Modelica模型
Modelica标准库里的异步电机模型过于复杂,为了便于学习,我用最基本的异步电机方程写了一个Modelica模型,公式参照陈伯时的<电力拖动自动控制系统--运动控制系统>第3版的190页 ...
- .NET MVC控制器向视图传递数据的四种方式
.NET MVC控制器向视图传递数据的四种方式: 1.ViewBag ViewBag.Mvc="mvc"; 2.ViewData ViewBag["Mvc"] ...
- 集DDD,TDD,SOLID,MVVM,DI,EF,Angularjs等于一身的.NET(C#)开源可扩展电商系统–Virto Commerce
今天一大早来看到园友分享的福利<分享一个前后端分离方案源码-前端angularjs+requirejs+dhtmlx 后端asp.net webapi>,我也来分享一个吧.以下内容由笔者写 ...