欧美成人午夜免费全部完,亚洲午夜福利精品久久,а√最新版在线天堂,另类亚洲综合区图片小说区,亚洲欧美日韩精品色xxx

扣丁學堂PHP培訓簡述Laravel源碼解析之路由的使用和示例詳解

2019-08-28 11:32:35 4925瀏覽

本篇文章扣丁學堂PHP培訓主要是給讀者們分享一下Laravel源碼解析之路由的使用和示例詳解,對Laravel源碼解析感興趣的小伙伴就隨小編來了解一下吧,希望對小伙伴們有幫助。


扣丁學堂PHP培訓簡述Laravel源碼解析之路由的使用和示例詳解


口:


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


注冊:


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


protected function mapApiRoutes()
{
  Route::prefix('api')
     ->middleware('api')
     ->namespace($this->namespace) // 設置所處命名空間
     ->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)有你所使用的所有路由相關方法,例如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;
}


添加后的結果如下圖所示:


PHP在線學習視頻


實例化:


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

想要了解更多關于PHP開發(fā)方面內(nèi)容的小伙伴,請關注扣丁學堂PHP培訓官網(wǎng)、微信等平臺,扣丁學堂IT職業(yè)在線學習教育有專業(yè)的PHP講師為您指導,此外扣丁學堂老師精心推出的PHP視頻教程定能讓你快速掌握PHP從入門到精通開發(fā)實戰(zhàn)技能??鄱W堂PHP技術交流群:374332265。


                          JavaEE/微服務/源碼解析/分布式/企業(yè)級架構【VIP體驗課】


     【關注微信公眾號獲取更多學習資料】        【掃碼進入JavaEE/微服務VIP免費公開課】  



查看更多關于“php培訓資訊”的相關文章>>

標簽: PHP培訓 PHP視頻教程 PHP在線視頻 PHP學習視頻 Laravel框架

熱門專區(qū)

暫無熱門資訊

課程推薦

微信
微博
15311698296

全國免費咨詢熱線

郵箱:codingke@1000phone.com

官方群:148715490

北京千鋒互聯(lián)科技有限公司版權所有   北京市海淀區(qū)寶盛北里西區(qū)28號中關村智誠科創(chuàng)大廈4層
京ICP備2021002079號-2   Copyright ? 2017 - 2022
返回頂部 返回頂部