Laravel 커널 인스턴스화에 대해 설명하는 기사

藏色散人
풀어 주다: 2021-09-06 09:07:29
앞으로
1497명이 탐색했습니다.

다음 Laravel 튜토리얼 칼럼에서는 Laravel 커널 인스턴스화 F에 대해 소개하겠습니다. 필요한 친구들에게 도움이 되었으면 좋겠습니다!

Laravel 커널 인스턴스화

$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
로그인 후 복사

Instantiation Kernel

애플리케이션이 인스턴스화되면 많은 기본 작업이 초기화되므로 다음 구성 방법에서는 서비스 컨테이너의 종속성 주입을 직접 사용하여 클래스 간의 종속성 문제를 해결합니다. .

// \Illuminate\Contracts\Http\Kernel 类构造器依赖 \Illuminate\Contracts\Foundation\Application 和 \Illuminate\Routing\Router,将会通过服务容器来处理依赖关系
public function __construct(Application $app, Router $router)
{
    $this->app = $app;

    // 主要委托 $router 来处理
    $this->router = $router;
    // 以下均为中间件的设置
    $router->middlewarePriority = $this->middlewarePriority;

    foreach ($this->middlewareGroups as $key => $middleware) {
        $router->middlewareGroup($key, $middleware);
    }

    foreach ($this->routeMiddleware as $key => $middleware) {
        $router->aliasMiddleware($key, $middleware);
    }
}

\Illuminate\Contracts\Foundation\Application 的处理:
make 时通过别名方式直接调用 $this->instances['app']

\Illuminate\Routing\Router 的处理:
make 时通过别名方式直接调用 $this->bindings['router'] 数组里面 concrete 对应的匿名函数
Router 依赖 \Illuminate\Contracts\Events\Dispatcher 和 \Illuminate\Container\Container
public function __construct(Dispatcher $events, Container $container = null)
{
    $this->events = $events;
    $this->routes = new RouteCollection;
    $this->container = $container ?: new Container;
}

\Illuminate\Contracts\Events\Dispatcher 的处理:
make 时通过别名方式直接调用 $this->bindings['events'] 数组里面 concrete 对应的匿名函数
Dispatcher 依赖 \Illuminate\Contracts\Container\Container
public function __construct(ContainerContract $container = null)
{
    $this->container = $container ?: new Container;
}

\Illuminate\Container\Container 的处理:
make 时直接调用 $this->instances['Illuminate\Container\Container'] = Object(app)
\Illuminate\Contracts\Container\Container 的处理:
make 时调用别名直接调用 $this->instances['app'] = Object(app)
上面两个一样,没有区别
로그인 후 복사

참고: 위에 나열된 종속성은 모두 자동 처리를 위해 서비스 컨테이너에 직접 위임됩니다. $this->bindings['router'] 및 $this-> 바인딩['이벤트']는 바인딩 이벤트를 처리합니다. 배열 키 콘크리트에 해당하는 익명 함수는 make 중에 직접 호출됩니다.

make

##############################################
if ($concrete instanceof Closure) {            
    return $concrete($this, end($this->with)); 
}
###############################################

$this->bindings['router'] = [
        'concrete' => function ($app) {
                            return new Router($app['events'], $app);
                        },
        'shared' => 'true',
    ];
$router = new Router($app['events'], $app);

\Illuminate\Routing\Router
public function __construct(Dispatcher $events, Container $container = null)
{
    $this->events = $events;
    $this->routes = new RouteCollection;
    $this->container = $container ?: new Container;
}
로그인 후 복사
동안 사용된 코드 조각은 Router 객체를 반환하는 동시에 다음번 직접 호출을 위해 $this->instances['router'] = $router 객체를 재설정합니다.

$this->bindings['events'] = [
    'concrete' => function ($app) {
            return (new Dispatcher($app))->setQueueResolver(function () use ($app) {
                return $app->make(QueueFactoryContract::class);
            });
            }
    'shared' => 'true',
];

$dispatcher = (new \Illuminate\Events\Dispatcher($app))->setQueueResolver(function () use ($app) {
                return $app->make(QueueFactoryContract::class);
            });

Illuminate\Events\Dispatcher:
public function __construct(ContainerContract $container = null)
{
    $this->container = $container ?: new Container;
}
public function setQueueResolver(callable $resolver)
{
    $this->queueResolver = $resolver;

    return $this;
}
로그인 후 복사
Dispatcher 객체를 반환하고 다음 번 직접 호출을 위해 $this->instances['events'] = $dispatcher 객체를 재설정합니다.

참고:

커널 객체는 애플리케이션과 라우팅을 결합한 객체이며, 라우팅은 핵심 객체인 IlluminateEventsDispatcher 객체를 주입합니다.


관련 권장 사항:
최신 5개 Laravel 비디오 튜토리얼

위 내용은 Laravel 커널 인스턴스화에 대해 설명하는 기사의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:segmentfault.com
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!