加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

Laravel生命周期与原理

发布时间:2020-05-25 08:20:01 所属栏目:PHP 来源:互联网
导读:一旦用户(浏览器)发送了一个HTTP请求,我们的apache或者nginx一般都转到index.php,因此,之后的一系列步骤都是从index.php开始的,我们先来看一看这个文件代码。 lt;?p


一旦用户(浏览器)发送了一个HTTP请求,我们的apache或者nginx一般都转到index.php,因此,之后的一系列步骤都是从index.php开始的,我们先来看一看这个文件代码。

<?php
require __DIR__.'/../bootstrap/autoload.php';
$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.
|
*/
$kernel = $app->make(IlluminateContractsHttpKernel::class);
$response = $kernel->handle(
  $request = IlluminateHttpRequest::capture()
);
$response->send();
$kernel->terminate($request,$response);

这里在注释里谈了kernel的作用,kernel处理来访的请求,并且发送相应返回给用户浏览器。

这里又涉及到了一个app对象,所以附上app对象的源码,这份源码是bootstrapapp.php

 1 <?php
 2 /*
 3 |--------------------------------------------------------------------------
 4 | Create The Application
 5 |--------------------------------------------------------------------------
 6 |
 7 | The first thing we will do is create a new Laravel application instance
 8 | which serves as the "glue" for all the components of Laravel,and is
 9 | the IoC container for the system binding all of the various parts.
10 |
11 */
12 $app = new IlluminateFoundationApplication(
13   realpath(__DIR__.'/../')
14 );
15 /*
16 |--------------------------------------------------------------------------
17 | Bind Important Interfaces
18 |--------------------------------------------------------------------------
19 |
20 | Next,we need to bind some important interfaces into the container so
21 | we will be able to resolve them when needed. The kernels serve the
22 | incoming requests to this application from both the web and CLI.
23 |
24 */
25 $app->singleton(
26   IlluminateContractsHttpKernel::class,27   AppHttpKernel::class
28 );
29 $app->singleton(
30   IlluminateContractsConsoleKernel::class,31   AppConsoleKernel::class
32 );
33 $app->singleton(
34   IlluminateContractsDebugExceptionHandler::class,35   AppExceptionsHandler::class
36 );
37 /*
38 |--------------------------------------------------------------------------
39 | Return The Application
40 |--------------------------------------------------------------------------
41 |
42 | This script returns the application instance. The instance is given to
43 | the calling script so we can separate the building of the instances
44 | from the actual running of the application and sending responses.
45 |
46 */
47 return $app;

请看app变量是IlluminateFoundationApplication类的对象,所以调用了这个类的构造函数,具体做了什么事,我们看源码。

1 public function __construct($basePath = null)
2 {
3   if ($basePath) {
4     $this->setBasePath($basePath);
5   }
6   $this->registerBaseBindings();
7   $this->registerBaseServiceProviders();
8   $this->registerCoreContainerAliases();
9 }

构造器做了3件事,前两件事很好理解,创建Container,注册了ServiceProvider,看代码

 1 /**
 2  * Register the basic bindings into the container.
 3  *
 4  * @return void
 5  */
 6 protected function registerBaseBindings()
 7 {
 8   static::setInstance($this);
 9   $this->instance('app',$this);
10   $this->instance(Container::class,$this);
11 }
12 /**
13  * Register all of the base service providers.
14  *
15  * @return void
16  */
17 protected function registerBaseServiceProviders()
18 {
19   $this->register(new EventServiceProvider($this));
20   $this->register(new LogServiceProvider($this));
21   $this->register(new RoutingServiceProvider($this));
22 }

最后一件事,是做了个很大的数组,定义了大量的别名,侧面体现程序员是聪明的懒人。

 1 /**
 2  * Register the core class aliases in the container.
 3  *
 4  * @return void
 5  */
 6 public function registerCoreContainerAliases()
 7 {
 8   $aliases = [
 9     'app'         => [IlluminateFoundationApplication::class,IlluminateContractsContainerContainer::class,IlluminateContractsFoundationApplication::class],10     'auth'         => [IlluminateAuthAuthManager::class,IlluminateContractsAuthFactory::class],11     'auth.driver'     => [IlluminateContractsAuthGuard::class],12     'blade.compiler'    => [IlluminateViewCompilersBladeCompiler::class],13     'cache'        => [IlluminateCacheCacheManager::class,IlluminateContractsCacheFactory::class],14     'cache.store'     => [IlluminateCacheRepository::class,IlluminateContractsCacheRepository::class],15     'config'        => [IlluminateConfigRepository::class,IlluminateContractsConfigRepository::class],16     'cookie'        => [IlluminateCookieCookieJar::class,IlluminateContractsCookieFactory::class,IlluminateContractsCookieQueueingFactory::class],17     'encrypter'      => [IlluminateEncryptionEncrypter::class,IlluminateContractsEncryptionEncrypter::class],18     'db'          => [IlluminateDatabaseDatabaseManager::class],19     'db.connection'    => [IlluminateDatabaseConnection::class,IlluminateDatabaseConnectionInterface::class],20     'events'        => [IlluminateEventsDispatcher::class,IlluminateContractsEventsDispatcher::class],21     'files'        => [IlluminateFilesystemFilesystem::class],22     'filesystem'      => [IlluminateFilesystemFilesystemManager::class,IlluminateContractsFilesystemFactory::class],23     'filesystem.disk'   => [IlluminateContractsFilesystemFilesystem::class],24     'filesystem.cloud'   => [IlluminateContractsFilesystemCloud::class],25     'hash'         => [IlluminateContractsHashingHasher::class],26     'translator'      => [IlluminateTranslationTranslator::class,IlluminateContractsTranslationTranslator::class],27     'log'         => [IlluminateLogWriter::class,IlluminateContractsLoggingLog::class,PsrLogLoggerInterface::class],28     'mailer'        => [IlluminateMailMailer::class,IlluminateContractsMailMailer::class,IlluminateContractsMailMailQueue::class],29     'auth.password'    => [IlluminateAuthPasswordsPasswordBrokerManager::class,IlluminateContractsAuthPasswordBrokerFactory::class],30     'auth.password.broker' => [IlluminateAuthPasswordsPasswordBroker::class,IlluminateContractsAuthPasswordBroker::class],31     'queue'        => [IlluminateQueueQueueManager::class,IlluminateContractsQueueFactory::class,IlluminateContractsQueueMonitor::class],32     'queue.connection'   => [IlluminateContractsQueueQueue::class],33     'queue.failer'     => [IlluminateQueueFailedFailedJobProviderInterface::class],34     'redirect'       => [IlluminateRoutingRedirector::class],35     'redis'        => [IlluminateRedisRedisManager::class,IlluminateContractsRedisFactory::class],36     'request'       => [IlluminateHttpRequest::class,SymfonyComponentHttpFoundationRequest::class],37     'router'        => [IlluminateRoutingRouter::class,IlluminateContractsRoutingRegistrar::class,IlluminateContractsRoutingBindingRegistrar::class],38     'session'       => [IlluminateSessionSessionManager::class],39     'session.store'    => [IlluminateSessionStore::class,IlluminateContractsSessionSession::class],40     'url'         => [IlluminateRoutingUrlGenerator::class,IlluminateContractsRoutingUrlGenerator::class],41     'validator'      => [IlluminateValidationFactory::class,IlluminateContractsValidationFactory::class],42     'view'         => [IlluminateViewFactory::class,IlluminateContractsViewFactory::class],43   ];
44   foreach ($aliases as $key => $aliases) {
45     foreach ($aliases as $alias) {
46       $this->alias($key,$alias);
47     }
48   }
49 }

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读