亚洲综合原千岁中文字幕_国产精品99久久久久久久vr_无码人妻aⅴ一区二区三区浪潮_成人h动漫精品一区二区三

主頁 > 知識庫 > Laravel框架生命周期與原理分析

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

熱門標簽:寶安400電話辦理 h5 地圖標注 高識別電銷機器人 電銷機器人-快迭智能 沈陽人工智能電銷機器人公司 合肥外呼系統app 拉薩打電話機器人 智能外呼電銷系統 哈爾濱400電話辦理到易號網

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

引言:

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

正文:

一旦用戶(瀏覽器)發送了一個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(Illuminate\Contracts\Http\Kernel::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 void
 */
protected function registerBaseBindings()
{
  static::setInstance($this);
  $this->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));
}

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

/**
 * Register the core class aliases in the container.
 *
 * @return void
 */
public 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 void
 */
public 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個單例模型對象,分別是HttpKernelConsoleKernelExceptionHandler。請注意,這里并沒有創建對象,只是聲明,也只是起了一個“別名”

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

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

?php
namespace 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 individually.
   *
   * @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 void
*/
public 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變量

更多關于Laravel相關內容感興趣的讀者可查看本站專題:《Laravel框架入門與進階教程》、《php優秀開發框架總結》、《php面向對象程序設計入門教程》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》

希望本文所述對大家基于Laravel框架的PHP程序設計有所幫助。

您可能感興趣的文章:
  • Laravel6.2中用于用戶登錄的新密碼確認流程詳解
  • Laravel 5框架學習之模型、控制器、視圖基礎流程
  • Laravel 5.4重新登錄實現跳轉到登錄前頁面的原理和方法
  • Laravel中間件實現原理詳解
  • Laravel模型事件的實現原理詳解
  • 淺談Laravel隊列實現原理解決問題記錄
  • Laravel框架隊列原理與用法分析
  • Laravel認證原理以及完全自定義認證詳解
  • laravel框架模型中非靜態方法也能靜態調用的原理分析
  • 淺談laravel aliases別名的原理
  • laravel 框架執行流程與原理簡單分析

標簽:林芝 成都 張家口 梅州 泰州 威海 巴中 山東

巨人網絡通訊聲明:本文標題《Laravel框架生命周期與原理分析》,本文關鍵詞  Laravel,框架,生命,周期,與,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Laravel框架生命周期與原理分析》相關的同類信息!
  • 本頁收集關于Laravel框架生命周期與原理分析的相關信息資訊供網民參考!
  • 推薦文章
    韩国三级香港三级日本三级| 欧美激情一区二区三区视频高清 | 日韩在线观看视频网站| 亚洲精品永久一区| 青青青草视频在线观看| 久久成人综合网| 一本高清在线| 国产精品1024在线永久免费| 99热精品一区| 毛片高清| 成人高清视频在线观看| 精品在线免费播放| 麻豆网站在线看| 好男人天堂网 久久精品国产这里是免费 国产精品成人一区二区 男人天堂网2021 男人的天堂在线观看 丁香六月综合激情 | 黄色免费三级| 国产精品免费久久| 日韩av成人| 亚欧成人乱码一区二区| 国产综合成人观看在线| 天天做日日爱夜夜爽| 精品国产一区二区三区精东影业| 午夜家庭影院| 深夜做爰性大片中文| 久久国产一久久高清| 日韩男人天堂| 日本在线播放一区| 夜夜操网| 99热视热频这里只有精品| 国产不卡在线观看视频| 久久精品成人一区二区三区| 国产精品自拍一区| 欧美一级视频高清片| 黄色福利| 在线观看成人网 | 午夜激情视频在线观看| 一级毛片视频免费| 国产国语对白一级毛片| 国产91素人搭讪系列天堂| 亚欧成人乱码一区二区| 精品久久久久久综合网| 日韩一级黄色片| 国产综合91天堂亚洲国产| 久久久久久久免费视频| 色综合久久天天综合绕观看 | 亚飞与亚基在线观看| 国产a视频| 国产精品123| 亚洲精品影院一区二区| 日日日夜夜操| 久久久久久久免费视频| 久久99欧美| 国产精品免费精品自在线观看| 久久精品免视看国产成人2021| 91麻豆精品国产综合久久久| 韩国毛片免费| 91麻豆精品国产自产在线观看一区| 在线观看成人网| 欧美大片a一级毛片视频| 999精品在线| 韩国三级一区| 欧美国产日韩久久久| 一 级 黄 中国色 片| 日韩在线观看视频黄| 欧美夜夜骑 青草视频在线观看完整版 久久精品99无色码中文字幕 欧美日韩一区二区在线观看视频 欧美中文字幕在线视频 www.99精品 香蕉视频久久 | 国产成a人片在线观看视频| 国产视频网站在线观看| 韩国三级一区| 日韩中文字幕一区| 国产不卡在线观看| 精品视频在线看 | 欧美另类videosbestsex高清| 国产网站在线| 九九久久99综合一区二区| 欧美爱色| 免费国产在线视频| 香蕉视频一级| 精品国产一区二区三区久| 成人免费观看视频| 色综合久久天天综合观看| 日韩一级黄色大片| 国产一区二区福利久久| 九九九国产| 国产不卡高清| 国产成人精品综合| 韩国三级香港三级日本三级| 国产一级生活片| 色综合久久手机在线| 欧美激情一区二区三区在线 | 国产成人精品综合| 久久国产一区二区| 亚洲www美色| 久久国产精品自由自在| 久久久久久久网| 国产一区二区精品尤物| 99色视频在线| 日本免费看视频| 午夜在线亚洲男人午在线| 午夜在线影院| 欧美激情一区二区三区视频 | 国产一区免费在线观看| 99久久精品国产国产毛片| 韩国三级视频网站| 美女免费黄网站| 国产91精品系列在线观看| 美女免费精品视频在线观看| 在线观看成人网| 日本在线播放一区| 国产精品123| 欧美电影免费| 日本免费乱理伦片在线观看2018| 日韩av成人| 99久久精品国产麻豆| 免费国产在线观看不卡| 欧美另类videosbestsex高清 | 精品国产一区二区三区精东影业 | 国产成人欧美一区二区三区的| 九九九网站| 国产成a人片在线观看视频| 欧美激情一区二区三区视频| 日韩在线观看视频免费| 日本特黄特黄aaaaa大片| 久久99欧美| 亚洲 激情| 亚欧成人乱码一区二区| 美国一区二区三区| 青青久热| 美女免费毛片| 四虎影视久久| 精品久久久久久免费影院| 韩国毛片免费| 青青久久网| 97视频免费在线观看| 韩国三级视频网站| 色综合久久手机在线| 欧美日本免费| 毛片高清| 一级毛片视频播放| 免费一级片在线| 一 级 黄 中国色 片| 国产极品精频在线观看| | 日韩一级黄色片| 日韩中文字幕在线播放| 国产网站免费观看| 精品在线观看一区| 成人影视在线观看| 日韩欧美一二三区| 精品国产一级毛片| 免费毛片播放| 麻豆午夜视频| 国产亚洲精品aaa大片| 国产一区二区精品在线观看| 精品视频一区二区三区免费| 麻豆网站在线看| 精品视频在线看 | 国产成人精品综合久久久| 深夜做爰性大片中文| 国产国语在线播放视频| 久草免费在线观看| a级毛片免费全部播放| 天天色色色| 日韩av东京社区男人的天堂| 国产亚洲精品成人a在线| 国产91精品一区二区| 一级毛片视频播放| 日日夜夜婷婷| 精品国产香蕉伊思人在线又爽又黄| 青青青草影院| 国产一区二区精品久久91| 国产91素人搭讪系列天堂| 国产a一级| 国产成人精品影视| 日韩女人做爰大片| 久久国产影院| 国产精品自拍在线观看| 欧美一级视| 99久久精品国产免费| 九九免费高清在线观看视频 | 国产视频一区在线| 一级毛片看真人在线视频| 日韩专区第一页| 日韩专区一区| 韩国三级视频网站| 欧美电影免费| 久久国产一区二区| 成人高清视频免费观看| 欧美a级大片| 国产一区免费在线观看| 久久精品欧美一区二区| 一级片片| 欧美国产日韩精品| 91麻豆精品国产自产在线| 999精品影视在线观看| 好男人天堂网 久久精品国产这里是免费 国产精品成人一区二区 男人天堂网2021 男人的天堂在线观看 丁香六月综合激情 | 欧美电影免费| 黄色福利片| 日韩专区亚洲综合久久| 国产欧美精品午夜在线播放| 亚洲第一色在线| 久久成人综合网|