亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 編程 > PHP > 正文

對于Laravel框架的生命周期與原理分析

2020-03-22 19:36:49
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了Laravel框架生命周期與原理,結合實例形式總結分析了Laravel框架針對用戶請求響應的完整運行周期、流程、原理,需要的朋友可以參考下

本文實例講述了Laravel框架生命周期與原理。分享給大家供大家參考,具體如下:

引言:

如果你對一件工具的使用原理了如指掌,那么你在用這件工具的時候會充滿信心!

正文:

一旦用戶(瀏覽器)發送了一個HTTP請求,我們的apache或者nginx一般都轉到index.php,因此,之后的一系列步驟都是從index.php開始的,我們先來看一看這個文件代碼。

 ?phprequire __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(Illuminate/Contracts/Http/Kernel::html' target='_blank'>class);$response = $kernel- handle( $request = Illuminate/Http/Request::capture()$response- send();$kernel- terminate($request, $response);

作者在注釋里談了kernel的作用,kernel的作用,kernel處理來訪的請求,并且發送相應返回給用戶瀏覽器。

這里又涉及到了一個app對象,所以附上app對象,所以附上app對象的源碼,這份源碼是/bootstrap/app.php

 ?php|--------------------------------------------------------------------------| Create The Application|--------------------------------------------------------------------------| The first thing we will do is create a new Laravel application instance| which serves as the glue for all the components of Laravel, and is| the IoC container for the system binding all of the various parts.$app = new Illuminate/Foundation/Application( realpath(__DIR__. /../ )|--------------------------------------------------------------------------| Bind Important Interfaces|--------------------------------------------------------------------------| Next, we need to bind some important interfaces into the container so| we will be able to resolve them when needed. The kernels serve the| incoming requests to this application from both the web and CLI.$app- 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類的對象,所以調用了這個類的構造函數,具體做了什么事,我們看源碼。

public function __construct($basePath = null) if ($basePath) { $this- setBasePath($basePath); $this- registerBaseBindings(); $this- registerBaseServiceProviders(); $this- registerCoreContainerAliases();}

構造器做了3件事,前兩件事很好理解,創建Container,注冊了ServiceProvider,看代碼

/** * Register the basic bindings into the container. * @return voidprotected function registerBaseBindings() static::setInstance($this); $this- instance( app , $this); $this- instance(Container::class, $this); * Register all of the base service providers. * @return voidprotected function registerBaseServiceProviders() $this- register(new EventServiceProvider($this)); $this- register(new LogServiceProvider($this)); $this- register(new RoutingServiceProvider($this));}

最后一件事,是做了個很大的數組,定義了大量的別名,側面體現程序員是聰明的懶人。

/** * Register the core class aliases in the container. * @return voidpublic function registerCoreContainerAliases() $aliases = [ app = [/Illuminate/Foundation/Application::class, /Illuminate/Contracts/Container/Container::class, /Illuminate/Contracts/Foundation/Application::class], auth = [/Illuminate/Auth/AuthManager::class, /Illuminate/Contracts/Auth/Factory::class], auth.driver = [/Illuminate/Contracts/Auth/Guard::class], blade.compiler = [/Illuminate/View/Compilers/BladeCompiler::class], cache = [/Illuminate/Cache/CacheManager::class, /Illuminate/Contracts/Cache/Factory::class], cache.store = [/Illuminate/Cache/Repository::class, /Illuminate/Contracts/Cache/Repository::class], config = [/Illuminate/Config/Repository::class, /Illuminate/Contracts/Config/Repository::class], cookie = [/Illuminate/Cookie/CookieJar::class, /Illuminate/Contracts/Cookie/Factory::class, /Illuminate/Contracts/Cookie/QueueingFactory::class], encrypter = [/Illuminate/Encryption/Encrypter::class, /Illuminate/Contracts/Encryption/Encrypter::class], db = [/Illuminate/Database/DatabaseManager::class], db.connection = [/Illuminate/Database/Connection::class, /Illuminate/Database/ConnectionInterface::class], events = [/Illuminate/Events/Dispatcher::class, /Illuminate/Contracts/Events/Dispatcher::class], files = [/Illuminate/Filesystem/Filesystem::class], filesystem = [/Illuminate/Filesystem/FilesystemManager::class, /Illuminate/Contracts/Filesystem/Factory::class], filesystem.disk = [/Illuminate/Contracts/Filesystem/Filesystem::class], filesystem.cloud = [/Illuminate/Contracts/Filesystem/Cloud::class], hash = [/Illuminate/Contracts/Hashing/Hasher::class], translator = [/Illuminate/Translation/Translator::class, /Illuminate/Contracts/Translation/Translator::class], log = [/Illuminate/Log/Writer::class, /Illuminate/Contracts/Logging/Log::class, /Psr/Log/LoggerInterface::class], mailer = [/Illuminate/Mail/Mailer::class, /Illuminate/Contracts/Mail/Mailer::class, /Illuminate/Contracts/Mail/MailQueue::class], auth.password = [/Illuminate/Auth/Passwords/PasswordBrokerManager::class, /Illuminate/Contracts/Auth/PasswordBrokerFactory::class], auth.password.broker = [/Illuminate/Auth/Passwords/PasswordBroker::class, /Illuminate/Contracts/Auth/PasswordBroker::class], queue = [/Illuminate/Queue/QueueManager::class, /Illuminate/Contracts/Queue/Factory::class, /Illuminate/Contracts/Queue/Monitor::class], queue.connection = [/Illuminate/Contracts/Queue/Queue::class], queue.failer = [/Illuminate/Queue/Failed/FailedJobProviderInterface::class], redirect = [/Illuminate/Routing/Redirector::class], redis = [/Illuminate/Redis/RedisManager::class, /Illuminate/Contracts/Redis/Factory::class], request = [/Illuminate/Http/Request::class, /Symfony/Component/HttpFoundation/Request::class], router = [/Illuminate/Routing/Router::class, /Illuminate/Contracts/Routing/Registrar::class, /Illuminate/Contracts/Routing/BindingRegistrar::class], session = [/Illuminate/Session/SessionManager::class], session.store = [/Illuminate/Session/Store::class, /Illuminate/Contracts/Session/Session::class], url = [/Illuminate/Routing/UrlGenerator::class, /Illuminate/Contracts/Routing/UrlGenerator::class], validator = [/Illuminate/Validation/Factory::class, /Illuminate/Contracts/Validation/Factory::class], view = [/Illuminate/View/Factory::class, /Illuminate/Contracts/View/Factory::class], foreach ($aliases as $key = $aliases) { foreach ($aliases as $alias) { $this- alias($key, $alias);}

這里出現了一個instance函數,其實這并不是Application類的函數,而是Application類的父類Container類的函數

/** * Register an existing instance as shared in the container. * @param string $abstract * @param mixed $instance * @return voidpublic function instance($abstract, $instance) $this- 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的對象,所以,新出現的singleton函數我們就可以到Container類的源代碼文件里查。bind函數和singleton的區別見這篇博文。

singleton這個函數,前一個參數是實際類名,后一個參數是類的“別名”。

$app對象聲明了3個單例模型對象,分別是HttpKernel,ConsoleKernel,ExceptionHandler。請注意,這里并沒有創建對象,只是聲明,也只是起了一個“別名”。

大家有沒有發現,index.php中也有一個$kernel變量,但是只保存了make出來的HttpKernel變量,因此本文不再討論,ConsoleKernel,ExceptionHandler。。。

繼續在文件夾下找到App/Http/Kernel.php,既然我們把實際的HttpKernel做的事情都寫在這個php文件里,就從這份代碼里看看究竟做了哪些事?

 ?phpnamespace App/Http;use Illuminate/Foundation/Http/Kernel as HttpKernel;class Kernel extends HttpKernel * The application s global HTTP middleware stack. * These middleware are run during every request to your application. * @var array protected $middleware = [ /Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode::class, ///App/Http/Middleware/MyMiddleware::class, * The application s route middleware groups. * @var array protected $middlewareGroups = [ web = [ /App/Http/Middleware/EncryptCookies::class, /Illuminate/Cookie/Middleware/AddQueuedCookiesToResponse::class, /Illuminate/Session/Middleware/StartSession::class, /Illuminate/View/Middleware/ShareErrorsFromSession::class, /App/Http/Middleware/VerifyCsrfToken::class, api = [ throttle:60,1 , * The application s route middleware. * These middleware may be assigned to groups or used inpidually. * @var array protected $routeMiddleware = [ auth = /App/Http/Middleware/Authenticate::class, auth.basic = /Illuminate/Auth/Middleware/AuthenticateWithBasicAuth::class, guest = /App/Http/Middleware/RedirectIfAuthenticated::class, throttle = /Illuminate/Routing/Middleware/ThrottleRequests::class, mymiddleware = /App/Http/Middleware/MyMiddleware::class,}

一目了然,HttpKernel里定義了中間件數組。

該做的做完了,就開始了請求到響應的過程,見index.php

$response = $kernel- handle( $request = Illuminate/Http/Request::capture()$response- send();

最后在中止,釋放所有資源。

/*** Call the terminate method on any terminable middleware.* @param /Illuminate/Http/Request $request* @param /Illuminate/Http/Response $response* @return voidpublic function terminate($request, $response) $this- terminateMiddleware($request, $response); $this- app- terminate();}

總結一下,簡單歸納整個過程就是:

1.index.php加載/bootstrap/app.php,在Application類的構造函數中創建Container,注冊了ServiceProvider,定義了別名數組,然后用app變量保存構造函數構造出來的對象。

2.使用app這個對象,創建1個單例模式的對象HttpKernel,在創建HttpKernel時調用了構造函數,完成了中間件的聲明。

3.以上這些工作都是在請求來訪之前完成的,接下來開始等待請求,然后就是:接受到請求-- 處理請求-- 發送響應-- 中止app變量

以上就是本文的全部內容,希望對大家的學習有所幫助,更多相關內容請關注PHP !

相關推薦:

Laravel框架實現利用中間件進行操作日志記錄功能

Laravel框架實現利用監聽器進行sql語句記錄功能

Laravel框架的路由設置

以上就是對于Laravel框架的生命周期與原理分析的詳細內容,PHP教程

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
欧美主播福利视频| 欧美成人免费va影院高清| 欧美日本中文字幕| 国产精品欧美一区二区| 精品国产网站地址| 亚洲综合精品伊人久久| 2019精品视频| 红桃视频成人在线观看| 91九色在线视频| 欧美成人免费在线观看| 国产91在线播放精品91| 97国产suv精品一区二区62| 欧美猛少妇色xxxxx| 亚洲精品suv精品一区二区| 欧美极度另类性三渗透| 国产精品www色诱视频| 国产精品成人一区二区三区吃奶| 日韩免费观看网站| 国产在线精品播放| 欧美激情亚洲综合一区| 一本色道久久88综合亚洲精品ⅰ| 一本色道久久88亚洲综合88| 国产欧美一区二区白浆黑人| 91最新国产视频| 欧美乱妇高清无乱码| 91精品国产91久久久久久久久| 成人免费视频xnxx.com| 日韩av毛片网| 2018中文字幕一区二区三区| 欧美极度另类性三渗透| 久久深夜福利免费观看| 国产日韩在线视频| 日韩欧美精品中文字幕| 蜜月aⅴ免费一区二区三区| 中文字幕视频在线免费欧美日韩综合在线看| 欧美中文字幕精品| 日本国产欧美一区二区三区| 亚洲综合自拍一区| 国产精品∨欧美精品v日韩精品| 欧美乱妇高清无乱码| 欧美午夜精品久久久久久人妖| 国产精品狼人色视频一区| 97超碰蝌蚪网人人做人人爽| 亚洲国产成人在线视频| 欧美寡妇偷汉性猛交| 亚洲精品一区久久久久久| 国模吧一区二区三区| 亚洲欧美另类在线观看| 一区二区欧美亚洲| 精品美女久久久久久免费| 午夜精品久久久久久久久久久久| 精品国产拍在线观看| 国内精品久久久久久中文字幕| 亚洲毛片在线免费观看| 神马久久久久久| 中文字幕免费精品一区高清| 久久久久一本一区二区青青蜜月| 亚洲国产三级网| 欧美一级视频在线观看| 成人网在线免费看| 成人免费网站在线观看| 亚洲综合色激情五月| 不卡中文字幕av| 欧美大片大片在线播放| xxxx性欧美| 热久久美女精品天天吊色| 狠狠久久亚洲欧美专区| 日韩精品在线第一页| 日韩性生活视频| 热久久免费国产视频| 北条麻妃一区二区在线观看| 日韩影视在线观看| 色一情一乱一区二区| 欧美大尺度电影在线观看| 成人看片人aa| 久久人91精品久久久久久不卡| 日韩va亚洲va欧洲va国产| 少妇精69xxtheporn| 亚洲福利视频网| 在线播放国产一区中文字幕剧情欧美| 欧美激情视频三区| 欧美日韩一区二区免费视频| 91九色国产视频| 久操成人在线视频| 国产69精品久久久久久| 亚洲视频一区二区| 成人欧美一区二区三区在线湿哒哒| 视频直播国产精品| 国产精品一区av| 欧美成人黄色小视频| 日韩av中文字幕在线免费观看| 精品无码久久久久久国产| 日韩久久精品电影| 日韩精品免费视频| 色与欲影视天天看综合网| 国产日韩中文在线| 55夜色66夜色国产精品视频| 国产精品一二区| 91九色单男在线观看| 亚洲国产成人久久综合一区| 国产精品入口尤物| 亚洲欧美日本伦理| 伊人久久男人天堂| 中文字幕一区电影| 欧美在线视频观看免费网站| 正在播放欧美视频| 91久久精品国产| 国产91精品在线播放| 国产精品扒开腿爽爽爽视频| 在线精品高清中文字幕| 精品国产91久久久久久老师| 亚洲成人教育av| 高清在线视频日韩欧美| 久久国产色av| 国产精品自产拍在线观| 久久在线免费视频| 美女999久久久精品视频| 亚洲成人黄色网址| 国产精品视频导航| 日本一区二区在线免费播放| 欧美日韩精品在线观看| 亚洲视频综合网| 亚洲人a成www在线影院| 97久久国产精品| 国内精品中文字幕| 成人国产精品色哟哟| 91国内揄拍国内精品对白| 45www国产精品网站| 日韩在线视频二区| 亚洲国产精品一区二区三区| 欧美日韩国产一区在线| 亚洲成年人在线播放| 国产精品精品一区二区三区午夜版| 亚洲欧美激情一区| 精品毛片网大全| 欧美性xxxxx极品娇小| 亚洲最大激情中文字幕| 亚洲综合一区二区不卡| 日韩av理论片| 日本久久久久久久| 日韩最新在线视频| 欧美日韩中文字幕综合视频| 97婷婷大伊香蕉精品视频| 国产精品中文字幕在线观看| 欧美激情极品视频| 成人在线一区二区| 97精品视频在线观看| 亚洲va久久久噜噜噜久久天堂| 亚洲精品在线不卡| 国产美女久久久| 日韩亚洲在线观看| 国产亚洲精品久久久久动| 午夜精品久久久久久久99热浪潮| 成人午夜高潮视频| 久久久久久高潮国产精品视| 久久香蕉频线观| 亚洲a成v人在线观看| 尤物九九久久国产精品的分类| 亚洲天堂男人的天堂| 欧美日韩一区二区三区在线免费观看| 97涩涩爰在线观看亚洲| 国产精品www网站| 亚洲精品理论电影| 国产精品亚洲аv天堂网|