加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Laravel框架生命周期与原理分析

发布时间:2020-12-14 20:05:14 所属栏目:大数据 来源:网络整理
导读:本篇章节讲解Laravel框架生命周期与原理。供大家参考研究具体如下: 引言: 如果你对一件工具的使用原理了如指掌,那么你在用这件工具的时候会充满信心! 正文: 一旦用户(浏览器)发送了一个HTTP请求,我们的apache或者nginx一般都转到index.php

本篇章节讲解Laravel框架生命周期与原理。分享给大家供大家参考,具体如下:

引言:

如果你对一件工具的使用原理了如指掌,那么你在用这件工具的时候会充满信心!

正文:

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

make(IlluminateContractsHttpKernel::class); $response = $kernel->handle( $request = IlluminateHttpRequest::capture() ); $response->send(); $kernel->terminate($request,$response);

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

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

singleton( IlluminateContractsHttpKernel::class,AppHttpKernel::class ); $app->singleton( IlluminateContractsConsoleKernel::class,AppConsoleKernel::class ); $app->singleton( IlluminateContractsDebugExceptionHandler::class,AppExceptionsHandler::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变量是

setBasePath($basePath); } $this->registerBaseBindings(); $this->registerBaseServiceProviders(); $this->registerCoreContainerAliases(); }

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

instance('app',$this); $this->instance(Container::class,$this); } /** * Register all of the base service providers. * * @return void */ protected function registerBaseServiceProviders() { $this->register(new EventServiceProvider($this)); $this->register(new LogServiceProvider($this)); $this->register(new RoutingServiceProvider($this)); }

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

[IlluminateFoundationApplication::class,IlluminateContractsContainerContainer::class,IlluminateContractsFoundationApplication::class],'auth' => [IlluminateAuthAuthManager::class,IlluminateContractsAuthFactory::class],'auth.driver' => [IlluminateContractsAuthGuard::class],'blade.compiler' => [IlluminateViewCompilersBladeCompiler::class],'cache' => [IlluminateCacheCacheManager::class,IlluminateContractsCacheFactory::class],'cache.store' => [IlluminateCacheRepository::class,IlluminateContractsCacheRepository::class],'config' => [IlluminateConfigRepository::class,IlluminateContractsConfigRepository::class],'cookie' => [IlluminateCookieCookieJar::class,IlluminateContractsCookieFactory::class,IlluminateContractsCookieQueueingFactory::class],'encrypter' => [IlluminateEncryptionEncrypter::class,IlluminateContractsEncryptionEncrypter::class],'db' => [IlluminateDatabaseDatabaseManager::class],'db.connection' => [IlluminateDatabaseConnection::class,IlluminateDatabaseConnectionInterface::class],'events' => [IlluminateEventsDispatcher::class,IlluminateContractsEventsDispatcher::class],'files' => [IlluminateFilesystemFilesystem::class],'filesystem' => [IlluminateFilesystemFilesystemManager::class,IlluminateContractsFilesystemFactory::class],'filesystem.disk' => [IlluminateContractsFilesystemFilesystem::class],'filesystem.cloud' => [IlluminateContractsFilesystemCloud::class],'hash' => [IlluminateContractsHashingHasher::class],'translator' => [IlluminateTranslationTranslator::class,IlluminateContractsTranslationTranslator::class],'log' => [IlluminateLogWriter::class,IlluminateContractsLoggingLog::class,PsrLogLoggerInterface::class],'mailer' => [IlluminateMailMailer::class,IlluminateContractsMailMailer::class,IlluminateContractsMailMailQueue::class],'auth.password' => [IlluminateAuthPasswordsPasswordBrokerManager::class,IlluminateContractsAuthPasswordBrokerFactory::class],'auth.password.broker' => [IlluminateAuthPasswordsPasswordBroker::class,IlluminateContractsAuthPasswordBroker::class],'queue' => [IlluminateQueueQueueManager::class,IlluminateContractsQueueFactory::class,IlluminateContractsQueueMonitor::class],'queue.connection' => [IlluminateContractsQueueQueue::class],'queue.failer' => [IlluminateQueueFailedFailedJobProviderInterface::class],'redirect' => [IlluminateRoutingRedirector::class],'redis' => [IlluminateRedisRedisManager::class,IlluminateContractsRedisFactory::class],'request' => [IlluminateHttpRequest::class,SymfonyComponentHttpFoundationRequest::class],'router' => [IlluminateRoutingRouter::class,IlluminateContractsRoutingRegistrar::class,IlluminateContractsRoutingBindingRegistrar::class],'session' => [IlluminateSessionSessionManager::class],'session.store' => [IlluminateSessionStore::class,IlluminateContractsSessionSession::class],'url' => [IlluminateRoutingUrlGenerator::class,IlluminateContractsRoutingUrlGenerator::class],'validator' => [IlluminateValidationFactory::class,IlluminateContractsValidationFactory::class],'view' => [IlluminateViewFactory::class,IlluminateContractsViewFactory::class],]; foreach ($aliases as $key => $aliases) { foreach ($aliases as $alias) { $this->alias($key,$alias); } } }

这里出现了一个instance函数,其实这并不是Application类的函数,而是Application类的父类Container类的函数

removeAbstractAlias($abstract); unset($this->aliases[$abstract]); // We'll check to determine if this type has been bound before,and if it has // we will fire the rebound callbacks registered with the container and it // can be updated with consuming classes that have gotten resolved here. $this->instances[$abstract] = $instance; if ($this->bound($abstract)) { $this->rebound($abstract); } }

Application是Container的子类,所以$app不仅是Application类的对象,还是Container的对象,所以,新出现的

$app对象声明了3个单例模型对象,分别是

大家有没有发现,index.php中也有一个$kernel变量,但是只保存了make出来的HttpKernel变量,因此本文不再讨论,ConsoleKernel,ExceptionHandler。。。

继续在文件夹下找到

[ AppHttpMiddlewareEncryptCookies::class,IlluminateCookieMiddlewareAddQueuedCookiesToResponse::class,IlluminateSessionMiddlewareStartSession::class,IlluminateViewMiddlewareShareErrorsFromSession::class,AppHttpMiddlewareVerifyCsrfToken::class,],'api' => [ 'throttle:60,1',]; /** * The application's route middleware. * * These middleware may be assigned to groups or used individually. * * @var array */ protected $routeMiddleware = [ 'auth' => AppHttpMiddlewareAuthenticate::class,'auth.basic' => IlluminateAuthMiddlewareAuthenticateWithBasicAuth::class,'guest' => AppHttpMiddlewareRedirectIfAuthenticated::class,'throttle' => IlluminateRoutingMiddlewareThrottleRequests::class,'mymiddleware'=>AppHttpMiddlewareMyMiddleware::class,]; }

一目了然,HttpKernel里定义了中间件数组。

该做的做完了,就开始了请求到响应的过程,见index.php

handle( $request = IlluminateHttpRequest::capture() ); $response->send();

最后在中止,释放所有资源。

terminateMiddleware($request,$response); $this->app->terminate(); }

总结一下,简单归纳整个过程就是:

1.index.php加载

2.使用app这个对象,创建1个单例模式的对象HttpKernel,在创建HttpKernel时调用了构造函数,完成了中间件的声明。

3.以上这些工作都是在请求来访之前完成的,接下来开始等待请求,然后就是:

更多关于Laravel相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》及《》

希望本文所述对大家基于Laravel框架的PHP程序设计有所帮助。

(编辑:李大同)

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

    推荐文章
      热点阅读