亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Laravel源碼解析之路由的使用和示例詳解

 更新時(shí)間:2018年09月27日 14:09:35   作者:CrazyCodes  
這篇文章主要介紹了Laravel源碼解析之路由的使用和示例詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

前言

我的解析文章并非深層次多領(lǐng)域的解析攻略。但是參考著開發(fā)文檔看此類文章會(huì)讓你在日常開發(fā)中更上一層樓。

廢話不多說,我們開始本章的講解。

入口

Laravel啟動(dòng)后,會(huì)先加載服務(wù)提供者、中間件等組件,在查找路由之前因?yàn)槲覀兪褂玫氖情T面,所以先要查到Route的實(shí)體類。

注冊(cè)

第一步當(dāng)然還是通過服務(wù)提供者,因?yàn)檫@是laravel啟動(dòng)的關(guān)鍵,在 RouteServiceProvider 內(nèi)加載路由文件。

protected function mapApiRoutes()
{
  Route::prefix('api')
     ->middleware('api')
     ->namespace($this->namespace) // 設(shè)置所處命名空間
     ->group(base_path('routes/api.php')); //所得路由文件絕對(duì)路徑
}

首先require是不可缺少的。因路由文件中沒有命名空間。 Illuminate\Routing\Router 下方法

protected function loadRoutes($routes)
{
  if ($routes instanceof Closure) {
    $routes($this);
  } else {
    $router = $this;

    require $routes;
  }
}

隨后通過路由找到指定方法,依舊是 Illuminate\Routing\Router 內(nèi)有你所使用的所有路由相關(guān)方法,例如get、post、put、patch等等,他們都調(diào)用了統(tǒng)一的方法 addRoute

public function addRoute($methods, $uri, $action)
{
  return $this->routes->add($this->createRoute($methods, $uri, $action));
}

之后通過 Illuminate\Routing\RouteCollection addToCollections 方法添加到集合中

protected function addToCollections($route)
{
  $domainAndUri = $route->getDomain().$route->uri();

  foreach ($route->methods() as $method) {
    $this->routes[$method][$domainAndUri] = $route;
  }

  $this->allRoutes[$method.$domainAndUri] = $route;
}

添加后的結(jié)果如下圖所示

實(shí)例化

依舊通過反射加載路由指定的控制器,這個(gè)時(shí)候build的參數(shù)$concrete = App\Api\Controllers\XxxController

public function build($concrete)
{
  // If the concrete type is actually a Closure, we will just execute it and
  // hand back the results of the functions, which allows functions to be
  // used as resolvers for more fine-tuned resolution of these objects.
  if ($concrete instanceof Closure) {
    return $concrete($this, $this->getLastParameterOverride());
  }
  
  $reflector = new ReflectionClass($concrete);
  // If the type is not instantiable, the developer is attempting to resolve
  // an abstract type such as an Interface of Abstract Class and there is
  // no binding registered for the abstractions so we need to bail out.
  if (! $reflector->isInstantiable()) {
    return $this->notInstantiable($concrete);
  }
  
    
  $this->buildStack[] = $concrete;

  $constructor = $reflector->getConstructor();
  // If there are no constructors, that means there are no dependencies then
  // we can just resolve the instances of the objects right away, without
  // resolving any other types or dependencies out of these containers.
  if (is_null($constructor)) {
  
      array_pop($this->buildStack);
  
      return new $concrete;
  }

  $dependencies = $constructor->getParameters();
  // Once we have all the constructor's parameters we can create each of the
  // dependency instances and then use the reflection instances to make a
  // new instance of this class, injecting the created dependencies in.
  $instances = $this->resolveDependencies(
    $dependencies
  );

  array_pop($this->buildStack);
  
  return $reflector->newInstanceArgs($instances);
}

這時(shí)將返回控制器的實(shí)例,下面將通過url訪問指定方法,一般控制器都會(huì)繼承父類 Illuminate\Routing\Controller ,laravel為其設(shè)置了別名 BaseController

public function dispatch(Route $route, $controller, $method)
{
  
  $parameters = $this->resolveClassMethodDependencies(
    $route->parametersWithoutNulls(), $controller, $method
  );

  if (method_exists($controller, 'callAction')) {

      return $controller->callAction($method, $parameters);
  }
    
  return $controller->{$method}(...array_values($parameters));
}

Laravel通過controller繼承的callAction去調(diào)用子類的指定方法,也就是我們希望調(diào)用的自定義方法。

public function callAction($method, $parameters)
{
  return call_user_func_array([$this, $method], $parameters);
}

致謝

感謝你看到這里,本篇文章源碼解析靠個(gè)人理解。如有出入請(qǐng)拍磚。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Laravel框架實(shí)現(xiàn)的記錄SQL日志功能示例

    Laravel框架實(shí)現(xiàn)的記錄SQL日志功能示例

    這篇文章主要介紹了Laravel框架實(shí)現(xiàn)的記錄SQL日志功能,結(jié)合實(shí)例形式總結(jié)分析了Laravel框架監(jiān)聽并記錄SQL相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下
    2018-06-06
  • 實(shí)例詳解PHP中html word 互轉(zhuǎn)的方法

    實(shí)例詳解PHP中html word 互轉(zhuǎn)的方法

    這篇文章主要介紹了實(shí)例詳解PHP中html word 互轉(zhuǎn)的方法的相關(guān)資料,涉及到php html word互轉(zhuǎn)的相關(guān)知識(shí),本文介紹的非常詳細(xì),具有參考借鑒價(jià)值
    2016-01-01
  • php打亂數(shù)組二維數(shù)組多維數(shù)組的簡(jiǎn)單實(shí)例

    php打亂數(shù)組二維數(shù)組多維數(shù)組的簡(jiǎn)單實(shí)例

    下面小編就為大家?guī)硪黄猵hp打亂數(shù)組二維數(shù)組多維數(shù)組的簡(jiǎn)單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-06-06
  • Yii框架Session與Cookie使用方法示例

    Yii框架Session與Cookie使用方法示例

    這篇文章主要介紹了Yii框架Session與Cookie使用方法,結(jié)合實(shí)例形式分析了Yii框架針對(duì)Session與Cookie的設(shè)置、獲取、刪除等相關(guān)操作技巧,需要的朋友可以參考下
    2019-10-10
  • php實(shí)現(xiàn)文件下載(支持中文文名)

    php實(shí)現(xiàn)文件下載(支持中文文名)

    這個(gè)下載實(shí)例結(jié)合header函數(shù)與while fread函數(shù)把文件分?jǐn)嘧x出來然后再發(fā)送到客戶端了,支持中文文名,算得上一個(gè)標(biāo)準(zhǔn)的文件下載實(shí)例
    2013-12-12
  • laravel實(shí)現(xiàn)前后臺(tái)路由分離的方法

    laravel實(shí)現(xiàn)前后臺(tái)路由分離的方法

    今天小編就為大家分享一篇laravel實(shí)現(xiàn)前后臺(tái)路由分離的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-10-10
  • PHP常量DIRECTORY_SEPARATOR原理及用法解析

    PHP常量DIRECTORY_SEPARATOR原理及用法解析

    這篇文章主要介紹了PHP常量DIRECTORY_SEPARATOR原理及用法解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-11-11
  • 阿里云服務(wù)器搭建Php+Apache運(yùn)行環(huán)境的詳細(xì)過程

    阿里云服務(wù)器搭建Php+Apache運(yùn)行環(huán)境的詳細(xì)過程

    這篇文章主要介紹了阿里云服務(wù)器搭建Php+Apache運(yùn)行環(huán)境,本文分步驟通過圖文實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),對(duì)阿里云服務(wù)器搭建php環(huán)境相關(guān)知識(shí)感興趣的朋友參考下吧
    2021-05-05
  • ThinkPHP的模版中調(diào)用session數(shù)據(jù)的方法

    ThinkPHP的模版中調(diào)用session數(shù)據(jù)的方法

    這篇文章主要介紹了ThinkPHP的模版中調(diào)用session數(shù)據(jù)的方法,需要的朋友可以參考下
    2014-07-07
  • 利用PHPStorm如何開發(fā)Laravel應(yīng)用詳解

    利用PHPStorm如何開發(fā)Laravel應(yīng)用詳解

    這篇文章主要給大家介紹了關(guān)于利用PHPStorm如何開發(fā)Laravel應(yīng)用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編來一起學(xué)習(xí)學(xué)習(xí)下吧。
    2017-08-08

最新評(píng)論