Laravel 소스 코드 분석 경로 의 사용 과 예제 에 대한 상세 한 설명
나의 해석 문장 은 깊 은 차원 의 여러 분야 의 해석 공략 이 아니다.하지만 개발 문 서 를 참고 해 이런 글 을 보면 일상적인 개발 에서 한 단계 더 나 아 갈 수 있다.
쓸데없는 말 을 많이 하지 말고 우 리 는 이 장의 설명 을 시작 합 시다.
입구
Laravel 이 시작 되면 서비스 제공 자,미들웨어 등 구성 요 소 를 먼저 불 러 옵 니 다.길 을 찾기 전에 저희 가 사용 하 는 것 이 외관 이기 때문에 Route 의 실체 류 를 먼저 찾 아야 합 니 다.
책.
첫 번 째 단 계 는 당연히 서비스 제공 자 를 통 해 시작 하 는 관건 이기 때문에
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);
}
사 의 를 표 하 다이 글 의 원본 코드 해석 은 개인 적 으로 이해 해 주 셔 서 감사합니다.출입 이 있 으 면 벽돌 을 찍 으 세 요.
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
단순 Laravel+Vue.js에서 VueUI를 사용하여 로그인 및 등록Laravel에는 웹 팩과 같은 Laravel-Mix가 있는데, 이를 이용해서 Laravel에 Vue.js를 실현할 수 있다. 이번에는 몇 가지 명령을 통해 간단하게 VueUI로 로그인하여 로그인을 할 수 있습니다....
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.