好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > Laravel源碼解析之路由的使用和示例詳解

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

熱門標簽:太原400電話申請流程 代理打電話機器人 企業400電話辦理多少費用 萍鄉商鋪地圖標注 神龍斗士電話機器人 電信外呼系統多少錢一個月 宿州正規外呼系統軟件 桂陽公司如何做地圖標注 合肥企業外呼系統線路

前言

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

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

入口

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

注冊

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

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 內有你所使用的所有路由相關方法,例如get、post、put、patch等等,他們都調用了統一的方法 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;
}

添加后的結果如下圖所示

實例化

依舊通過反射加載路由指定的控制器,這個時候build的參數$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去調用子類的指定方法,也就是我們希望調用的自定義方法。

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

致謝

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • Laravel框架源碼解析之入口文件原理分析
  • Laravel框架源碼解析之反射的使用詳解
  • 通過源碼解析Laravel的依賴注入
  • Laravel框架學習筆記(二)項目實戰之模型(Models)
  • laravel model模型定義實現開啟自動管理時間created_at,updated_at
  • laravel model模型處理之修改查詢或修改字段時的類型格式案例
  • Laravel5.1 框架模型工廠ModelFactory用法實例分析
  • Laravel 5框架學習之模型、控制器、視圖基礎流程
  • Laravel模型事件的實現原理詳解
  • Laravel模型間關系設置分表的方法示例
  • laravel學習教程之關聯模型
  • Laravel框架源碼解析之模型Model原理與用法解析

標簽:白銀 太原 辛集 衡陽 鄂州 廊坊 綏化 崇左

巨人網絡通訊聲明:本文標題《Laravel源碼解析之路由的使用和示例詳解》,本文關鍵詞  Laravel,源碼,解析,之,路由,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Laravel源碼解析之路由的使用和示例詳解》相關的同類信息!
  • 本頁收集關于Laravel源碼解析之路由的使用和示例詳解的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 91精品国产综合aV| 小莹回乡纵欢老刘头| 亚洲人成色99999在线观看| 久久久久久精品无码嘿嘿嘿嘿| 用力操啊啊啊| 大象影视网大全| 波多野结衣巜人妻性努力| 爱情岛论坛亚洲线路一| 青青艹在线观看| 色黄网站成年女人色毛片| 公交忘穿内裤揉捏呻吟白浆| 爽?好大?快?深点樱花| 亚洲黄色三级电影| 精品一区二区三区视频| 诱大美腿丝袜美腿丝袜美腿| 高清欧美不卡一区二区三区| 色日本在线| 我的空姐女友| mm1314极品翘臀尤物美女图片 | 能播放女人另类zooz0| 老师用力挺进小雪肉便动视频| 国产精品人妻无码一区牛牛影视| 办公室大战秘书呻吟| 波多野结衣之cesd819| 亚洲 欧美 偷拍 丝袜 清| 欧美胖乎乎的肥妇BBW| 黄色带电在线视频| 人妖丝袜变装被c好爽| 日本国产亚洲| 冲田杏梨一二三区毛片| 国产一区二区三区美女在线观看| 女人脱了内裤让男人猛戳 | 麻豆精产国品一二三产区区| 精品国产福利第一区二区三区| 成人动漫在线观看视频| 天天操夜夜摸| ⅩXXXSSSS性sex国产| 国产伦精品一区二区三区视频黑人| 黄色app大全| 国产1卡二卡3卡四卡免费| 色毛片|