您好,欢迎访问一九零五行业门户网

laravel5源码讲解整理,laravel5源码讲解_PHP教程

laravel5源码讲解整理,laravel5源码讲解来源:http://yuez.me/laravel-yuan-ma-jie-du/?utm_source=tuicool&utm_medium=referral目录入口文件 index.phpilluminate\foundation\application 类注入所有基础 service provider 
入口文件 index.php一个基于laravel的应用,当web服务器接受到来自外部的请求后,会将这个这个请求解析到 应用根目录的 public/index.php 中。
laravel源码解读-index.php (laravel_index.php)download123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
make(illuminate\contracts\http\kernel::class);$response = $kernel->handle( $request = illuminate\http\request::capture());$response->send();$kernel->terminate($request, $response);
第二十一行代码
1

require __dir__.'/../bootstrap/autoload.php';
为laravel应用引入了由composer提供的类加载器,这样laravel应用便无需再手动加载任 何的类。其加载原理不是此次探究的目标,所以仅仅这样使用就好了。接下的代码,便是重 点。
illuminate\foundation\application 类该类的继承结构如下:
第三十五行代码
1

$app = require_once __dir__.'/../bootstrap/app.php';
它将我的视线引入到了另外一个文件中,去看看到底发生了什么吧。
laravel源码解读-app.php (laravel_app.php)download12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
singleton( illuminate\contracts\http\kernel::class, app\http\kernel::class);$app->singleton( illuminate\contracts\console\kernel::class, app\console\kernel::class);$app->singleton( illuminate\contracts\debug\exceptionhandler::class, app\exceptions\handler::class);/*|--------------------------------------------------------------------------| return the application|--------------------------------------------------------------------------|| this script returns the application instance. the instance is given to| the calling script so we can separate the building of the instances| from the actual running of the application and sending responses.|*/return $app;
看第十四行,原来$app是一个 illuminate\foundation\application 对象,那么在创 建这个对象的时候又发生了什么呢?
从它的构造方法看起:
illuminate\foundation\application 构造方法123456789101112131415161718
/** * create a new illuminate application instance. * * @param string|null $basepath * @return void */public function __construct($basepath = null){ $this->registerbasebindings(); $this->registerbaseserviceproviders(); $this->registercorecontaineraliases(); if ($basepath) { $this->setbasepath($basepath); }}
顺着函数调用,往下看。在这个构造函数中,首先调用了registerbasebindings方法。
illuminate\foundation\application#registerbasebindings12345678910111213
/** * register the basic bindings into the container. * * @return void */protected function registerbasebindings(){ static::setinstance($this); $this->instance('app', $this); $this->instance('illuminate\container\container', $this);}
这段代码,是将实例对象注入到容器中。那么,这个容器是什么呢?答案还是要从这段调用 中去寻找。
static::setinstance($this) 所做的就是将 $this 赋值给自身的 instance 静态变 量。重点看 $this->instance('app', $this)。
instance 函数的作用是绑定一个已有对象到容器中,这个对象在容器中共享并且可以通 过键获取。
illuminate\container\container#instance1234567891011121314151617181920212223242526272829
/** * register an existing instance as shared in the container. * * @param string $abstract * @param mixed $instance * @return void */public function instance($abstract, $instance){ if (is_array($abstract)) { // $abstract 是这样的一个数组 ['actual key' => 'alias'] list($abstract, $alias) = $this->extractalias($abstract); // 实际上的行为是 $this->aliases[$alias] = $abstract; $this->alias($abstract, $alias); } unset($this->aliases[$abstract]); // 检查是否有这个键是否已经注册到容器中 // $bound 是一个boolean值 $bound = $this->bound($abstract); $this->instances[$abstract] = $instance; if ($bound) { $this->rebound($abstract); }}
视线重新回到application类中,接下来调用了这个方法 $this->registerbaseserviceproviders(),
illuminate\foundation\application#registerbaseserviceproviders1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
/** * register all of the base service providers. * * @return void */protected function registerbaseserviceproviders(){ $this->register(new eventserviceprovider($this)); $this->register(new routingserviceprovider($this));}/** * register a service provider with the application. * * @param \illuminate\support\serviceprovider|string $provider * @param array $options * @param bool $force * @return \illuminate\support\serviceprovider */public function register($provider, $options = [], $force = false){ if ($registered = $this->getprovider($provider) && !$force) { return $registered; } // if the given provider is a string, we will resolve it, passing in the // application instance automatically for the developer. this is simply // a more convenient way of specifying your service provider classes. if (is_string($provider)) { $provider = $this->resolveproviderclass($provider); } $provider->register(); // once we have registered the service we will iterate through the options // and set each of them on the application so they will be available on // the actual loading of the service objects and for developer usage. foreach ($options as $key => $value) { $this[$key] = $value; } $this->markasregistered($provider); // if the application has already booted, we will call this boot method on // the provider class so it has an opportunity to do its boot logic and // will be ready for any usage by the developer's application logics. if ($this->booted) { $this->bootprovider($provider); } return $provider;}
其中,eventserviceprovider和routingserviceprovider分别是
illuminate\events\eventserviceproviderilluminate\routing\routingserviceprovider这些serviceprovider是 illuminate\support\serviceprovider 的子类,它接受一个 application 对象作为构造函数参数,存储在实例变量 $app 中。
注入所有基础 service provider在 register 方法中,每个serviceprovider被调用了自身的 register 方法。首先看 看 eventserviceprovider 中的吧。
illuminate\events\eventserviceprovider#register12345678
public function register(){ $this->app->singleton('events', function ($app) { return (new dispatcher($app))->setqueueresolver(function () use ($app) { return $app->make('illuminate\contracts\queue\factory'); }); });}
上面方法体将一个 illuminate\events\dispatcher 对象以键 events 绑定到了容器 中,它负责实现事件的调度。
再看看 illuminate\routing\routingserviceprovider:
illuminate\routing\routingserviceprovider#register1234567891011121314
public function register(){ $this->registerrouter(); $this->registerurlgenerator(); $this->registerredirector(); $this->registerpsrrequest(); $this->registerpsrresponse(); $this->registerresponsefactory();}
首页是在laravel中接触的最多的 route 被注册,它是 illuminate\routing\router 对象。
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
其他好文
http://www.cnblogs.com/wish123/p/4756669.html
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
http://www.bkjia.com/phpjc/1071501.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/1071501.htmltecharticlelaravel5源码讲解整理,laravel5源码讲解 来源:http://yuez.me/laravel-yuan-ma-jie-du/?utm_source=tuicoolutm_medium=referral 目录 入口文件 index.php illuminate\fou...
其它类似信息

推荐信息