フレームワークLaravel下経路(ソースコード分析)を用いて

この記事の内容は、私たちは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;

}

図に示す以下の結果を追加します。

2988532948-5bac40d3bd9b4_articlex.png

コール

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() // 此处调用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]; // 0=>控制器 xxController 1=>方法名 index

        $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 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

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);

}

おすすめ

転載: www.cnblogs.com/it-3327/p/11795668.html