laravel 프레임 워 크 실행 절차 와 원리 에 대한 간단 한 분석

본 논문 의 사례 는 laravel 프레임 워 크 의 집행 절차 와 원 리 를 서술 하 였 다.여러분 께 참고 하도록 공유 하 겠 습 니 다.구체 적 으로 는 다음 과 같 습 니 다.
1.index.php

$app = require_once __DIR__.'/../bootstrap/app.php';
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
$response = $kernel->handle(
  $request = Illuminate\Http\Request::capture()
);

2.app.php 에 들 어가 기

$app = new Illuminate\Foundation\Application(
  $_ENV['APP_BASE_PATH'] ?? dirname(__DIR__)
);
$app->singleton(
  Illuminate\Contracts\Http\Kernel::class,
  App\Http\Kernel::class
);

응용 프로그램 클래스 바 인 딩 기본 클래스 용기
Kernel 클래스 실행 경로 배포 로드 컨트롤 러 등 작업
3.Kernel.php 진입

//        ,        
protected $middleware = [];
//        ,
protected $middlewareGroups = [];
//      ,             
protected $routeMiddleware = [];
//      
protected $middlewarePriority = [];

kernel 클래스 계승 Illuminate\Foundation\Http\\Kernel 클래스
4.Illuminate\Foundation\Http\\Kernel 클래스 에 들 어가 기

//http      ,    X-HTTP-METHOD-OVERRIDE     ,         ,    ,   post
$request->enableHttpMethodParameterOverride();
//      /          。
$response = $this->sendRequestThroughRouter($request);
//             :     
$this->app['events']->dispatch(
new Events\RequestHandled($request, $response)
);
return $response;

sendRequestThrough Router 에 들 어 가 는 방법

//          
 $this->app->instance('request', $request);
//  facade           
Facade::clearResolvedInstance('request');
//        protected $bootstrappers = [];           
$this->bootstrap();
//             ,    ,aop 
 return (new Pipeline($this->app))
          //            
          ->send($request)
          //       。
          // shouldSkipMiddleware             ,$this->make('middleware.disable')    true
          // $this->bound('middleware.disable')    true             ,            ?
          ->through($this->app->shouldSkipMiddleware() ? [] : $this->middleware)
          //             ,         
          ->then($this->dispatchToRouter());

dispatchToRouter 경로 에 들 어가 배포 하고 최종 적 으로 경로 류 에서 dispatch 실행 경로 에 들 어가 주입 을 실현 합 니 다.

$this->router->dispatch($request);
반사 원 리 를 이용 하여 Illuminate\Container\\Container 류 에 의존 하 는 것 을 실현 합 니 다.

public function make($abstract, array $parameters = []){
  return $this->resolve($abstract, $parameters);
}
public function build($concrete)
// build  
$reflector = new ReflectionClass($concrete);

의존 주입 은$parameter->getClass()를 통 해 어떤 종류 인지 알 수 있 습 니 다.

class Demo{
  public function store(Request $req333, $abc){
  }
}
class Request{}
$method = new ReflectionMethod('Demo', 'store');
foreach ($method->getParameters() as $parameter) {
  //            
  $param_type = $param->getClass(); //             
  $param_value = $param->getName(); //      
  if ($param_type) {
    //                    
    $avgs[] = $app[$param_type->name];
  }
}
$reflect->invokeArgs($app['demo'], $avgs);

X-HTTP-METHOD-OVERRIDE 는 laravel 의 요청 을 덮어 쓸 수 있 습 니 다.예 를 들 어 restful 요청,put delete 등 특수 한 요청 을 수행 할 수 있 습 니 다.
Laravel 관련 내용 에 관심 이 있 는 독자 들 은 본 사이트 의 주 제 를 볼 수 있다.
본 고 는 Laravel 프레임 워 크 를 바탕 으로 하 는 PHP 프로 그래 밍 에 도움 이 되 기 를 바 랍 니 다.

좋은 웹페이지 즐겨찾기