借鉴:

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 启动详解的更多相关文章

  1. laravel5.1启动详解

    laravel的启动过程 如果没有使用过类似Yii之类的框架,直接去看laravel,会有点一脸迷糊的感觉,起码我是这样的.laravel的启动过程,也是laravel的核心,对这个过程有一个了解,有 ...

  2. 深入浅出mybatis之启动详解

    深入浅出mybatis之启动详解 MyBatis功能丰富,但使用起来非常简单明了,今天我们来追踪一下它的启动过程. 目录 如何启动MyBatis 如何使用MyBatis MyBatis启动过程 如何启 ...

  3. Linux 启动详解之init

    1.init初探 init是Linux系统操作中不可缺少的程序之一.init进程,它是一个由内核启动的用户级进程,然后由它来启动后面的任务,包括多用户环境,网络等. 内核会在过去曾使用过init的几个 ...

  4. Linux启动详解

    <概述> Linux启动大致分为一下几个步骤,详细的启动步骤在<启动分析>中详解. 1:首先bios加电自检,初始化(这个过程会检测相关硬件(cpu,内存,显卡,硬盘等)) 2 ...

  5. Linux开机启动详解

    Linux开机启动程序详解 我们假设大家已经熟悉其它操作系统的引导过程,了解硬件的自检引导步骤,就只从Linux操作系统的引导加载程序(对个人电脑而言通常是LILO)开始,介绍Linux开机引导的步骤 ...

  6. Hadoop- 集群启动详解

    NameNode启动过程详解 第一次启动:HDFS格式化后,生成fsimage文件 hdf

  7. Mongodb安装启动详解

    最近在倒腾node+mongodb,安装mongodb的时候开始遇到很多问题,然后折腾了好几次,直到可以很顺利完成安装 ,所以把安装的过程记录下来. 线上系统基本上都是linux的,所以只安装了lin ...

  8. spring boot容器启动详解

    目录 一.前言 二.容器启动 三.总结 =======正文分割线====== 一.前言 spring cloud大行其道的当下,如果不了解基本原理那么是很纠结的(看见的都是约定大于配置,但是原理呢?为 ...

  9. Springboot 启动详解

    1.前言 最近一直在看Springboot和springcloud代码,看了将近20多天,对这两个系统的认知总算是入了门.后续应该会有一个系列的文章,本文就先从Springboot的启动入手. 2.容 ...

随机推荐

  1. 10Spring_AOP编程(传统编程)

    注意我写这篇文章的思路,要想做切面编程,包含两个部分,通知和切点,通知是你要做哪些增强,切点是指你要拦截哪些方法.先介绍通知的定义再去介绍切点的定义.这篇文章我取名叫做Spring_AOP编程(传统编 ...

  2. Android开发eclipse运行程序时报timeout的解决方法

    eclipse开发Android程序时,忽然莫名其妙报这个错,之前还好好的.忽然就不行了. Failed to install xxx.apk on device~~~: timeout 尝试过手机里 ...

  3. Contains Duplicate

    Given an array of integers, find if the array contains any duplicates. Your function should return t ...

  4. 基于jsp的文件上传和下载

    参考: 一.JavaWeb学习总结(五十)--文件上传和下载 此文极好,不过有几点要注意: 1.直接按照作者的代码极有可能listfile.jsp文件中 <%@taglib prefix=&qu ...

  5. [CareerCup] 1.8 String Rotation 字符串的旋转

    1.8 Assume you have a method isSubstring which checks if one word is a substring of another. Given t ...

  6. 更简单地进行Auto Layout--SnapKit 支持swift

    OC下的autolayout神器Masonry大家已经很熟悉了.但是masonry在swift下使用并不方便.所以同一个团队开发出了swift下的autolayout库:SnapKitsnapkit从 ...

  7. ios UILocalNotification的使用

    iOS下的Notification的使用 Notification是智能手机应用编程中非常常用的一种传递信息的机制,而且可以非常好的节省资源,不用消耗资源来不停地检查信息状态(Pooling),在iO ...

  8. 使用AChartEngine第一步:在项目中配置AChartEngine环境

    1. 从AChartEngine官网上下载acharengine的jar文件包. 地址:https://code.google.com/p/achartengine/downloads/list 2. ...

  9. Opencv step by step - 鼠标事件

    鼠标事件有下面几种(没有滚轮事件,比较遗憾): #define CV_EVENT_MOUSEMOVE 0 滑动 #define CV_EVENT_LBUTTONDOWN 1 左键点击 #define ...

  10. maven编译设置pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...