国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - PHP教程 - Laravel源碼解析之路由的使用和示例詳解

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

2019-09-05 11:44CrazyCodes PHP教程

這篇文章主要介紹了Laravel源碼解析之路由的使用和示例詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

前言

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

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

入口

Laravel啟動后,會先加載服務(wù)提供者、中間件等組件,在查找路由之前因為我們使用的是門面,所以先要查到Route的實體類。

注冊

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

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

首先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é)果如下圖所示

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

實例化

依舊通過反射加載路由指定的控制器,這個時候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);
}

這時將返回控制器的實例,下面將通過url訪問指定方法,一般控制器都會繼承父類 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);
}

致謝

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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲成人激情在线 | 午夜精品久久久久久久白皮肤 | 美女h视频 | 成人久久久久久久 | 成人免费在线视频播放 | 亚洲天堂网站 | 中文字幕日韩欧美 | 久久精品国产99精品国产亚洲性色 | 精品亚洲一区二区 | 国产女精品 | 国产精品美女久久久久久久网站 | 亚洲精品欧美精品 | 国产精品成人在线 | 欧美片网站免费 | 综合久久网 | 天天看夜夜| 福利片一区二区 | 一区二区三区免费观看 | 国产精品综合一区二区 | 女人夜夜春高潮爽av片 | 黄色国产大片 | 99久久国产免费 | av观看免费 | 91久久久久久久久 | 国产成人精品一区二区三区四区 | 亚洲一级片av | 亚洲久草 | 欧美日韩中文字幕在线 | 黄色短视频在线观看 | 亚洲精品99 | 日韩视频一区 | 久久久久久久久99精品 | 亚洲一区二区三区久久久 | 国产精品视频一区二区三区不卡 | 精品国产欧美一区二区 | 色五月激情综合网 | 激情久久久| 精品福利一区二区三区免费视频 | 欧美精品一区自拍a毛片在线视频 | 亚洲va国产va天堂va久久 | 无码日韩精品一区二区免费 |