この記事の内容は、私たちはLaravelフレームワーク(ソースコードの解析)の下のルートで使用与えることで、いくつかの基準値を参照することができます必要としている友人がいる、私はあなたの助けを願っています。
序文
私は深いの様々な分野でレイダーズ記事を解決しない解決。しかし、参考文献の開発に、このような記事があなたの毎日の開発に良く見えるようになります。
騒ぎが、我々は、この章で説明し始めました。
エントランス
Laravelが起動した後、我々はファサードを使用しているため、ルーティング探す前に、サービスプロバイダ、ミドルウェア・コンポーネントをロードする前に、その最初のルートエンティティクラスを発見しました。
登録
これがあるため、サービスプロバイダによるコースの最初のステップは、キーlaravelはで始まっ RouteServiceProvider
負荷が内部ファイルをルーティングします。
1 2 3 4 5 6 7 |
protected function mapApiRoutes()
{
Route::prefix( 'api' )
->middleware( 'api' )
-> namespace ( $this -> namespace )
->group(base_path( 'routes/api.php' ));
}
|
まず必要不可欠です。何のルーティングファイルの名前空間がありませんので。 Illuminate\Routing\Router
法の下で
1 2 3 4 5 6 7 8 9 10 |
protected function loadRoutes( $routes )
{
if ( $routes instanceof Closure) {
$routes ( $this );
} else {
$router = $this ;
require $routes ;
}
}
|
それまでに指定したルーティング方法を見つけ、まだ Illuminate\Routing\Router
そのようGET、POST、PUT、パッチなどとして使用するすべての関連ルーティング方法が存在している、彼らは統一されたアプローチを呼び出しています addRoute
1 2 3 4 |
public function addRoute( $methods , $uri , $action )
{
return $this ->routes->add( $this ->createRoute( $methods , $uri , $action ));
}
|
通過した後、 Illuminate\Routing\RouteCollection
収集方法のaddToCollectionsへ
1 2 3 4 5 6 7 8 9 10 |
protected function addToCollections( $route )
{
$domainAndUri = $route ->getDomain(). $route ->uri();
foreach ( $route ->methods() as $method ) {
$this ->routes[ $method ][ $domainAndUri ] = $route ;
}
$this ->allRoutes[ $method . $domainAndUri ] = $route ;
}
|
図に示す以下の結果を追加します。
コール
Illuminate\Routing\Router
メソッドでインスタンス化ルーティングロジックの実行を開始
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
protected function runRoute(Request $request , Route $route )
{
$request ->setRouteResolver( function () use ( $route ) {
return $route ;
});
$this ->events->dispatch( new Events\RouteMatched( $route , $request ));
return $this ->prepareResponse( $request ,
$this ->runRouteWithinStack( $route , $request )
);
}
....
protected function runRouteWithinStack(Route $route , Request $request )
{
$shouldSkipMiddleware = $this ->container->bound( 'middleware.disable' ) &&
$this ->container->make( 'middleware.disable' ) === true;
$middleware = $shouldSkipMiddleware ? [] : $this ->gatherRouteMiddleware( $route );
return ( new Pipeline( $this ->container))
->send( $request )
->through( $middleware )
->then( function ( $request ) use ( $route ) {
return $this ->prepareResponse(
$request , $route ->run()
);
});
}
|
Illuminate\Routing\Route
正方形実行制御を行うための方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public function run()
{
$this ->container = $this ->container ?: new Container;
try {
if ( $this ->isControllerAction()) {
return $this ->runController();
}
return $this ->runCallable();
} catch (HttpResponseException $e ) {
return $e ->getResponse();
}
}
|
上記プロセスからわかるように、キーrunController実行経路、スケジューラ法、コントローラの実行中である $this->getController()
と制御の方法 $this->getControllerMethod()
に渡される dispatch
スケジューリング方法は
1 2 3 4 5 6 7 |
protected function runController()
{
return $this ->controllerDispatcher()->dispatch(
$this , $this ->getController(), $this ->getControllerMethod()
);
}
|
なお、ここでは getController()
、実際のコントローラメソッドをインスタンス化
1 2 3 4 5 6 7 8 9 10 |
public function getController()
{
if (! $this ->controller) {
$class = $this ->parseControllerCallback()[0];
$this ->controller = $this ->container->make(ltrim( $class , '\\' ));
}
return $this ->controller;
}
|
インスタンス化
それはまだ反射ロードされた指定されたコントローラを介してルーティングされ、今回のビルドパラメータ$コンクリート= App\Api\Controllers\XxxController
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
public function build( $concrete )
{
if ( $concrete instanceof Closure) {
return $concrete ( $this , $this ->getLastParameterOverride());
}
$reflector = new ReflectionClass( $concrete );
if (! $reflector ->isInstantiable()) {
return $this ->notInstantiable( $concrete );
}
$this ->buildStack[] = $concrete ;
$constructor = $reflector ->getConstructor();
if ( is_null ( $constructor )) {
array_pop ( $this ->buildStack);
return new $concrete ;
}
$dependencies = $constructor ->getParameters();
$instances = $this ->resolveDependencies(
$dependencies
);
array_pop ( $this ->buildStack);
return $reflector ->newInstanceArgs( $instances );
}
|
这时将返回控制器的实例,下面将通过url访问指定方法,一般控制器都会继承父类 Illuminate\Routing\Controller
,laravel为其设置了别名 BaseController
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
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去调用子类的指定方法,也就是我们希望调用的自定义方法。
1 2 3 4 |
public function callAction( $method , $parameters )
{
return call_user_func_array([ $this , $method ], $parameters );
}
|