Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of kernel instantiation in Laravel

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail the example analysis of kernel instantiation in Laravel. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

Laravel kernel instantiation

$kernel = $app- > make (Illuminate\ Contracts\ Http\ Kernel::class); instantiate Kernel

Many basic operations have been initialized when the application is instantiated, so the following constructor will directly use the dependency injection of the service container to solve the dependencies between classes.

/ /\ Illuminate\ Contracts\ Http\ Kernel class constructor relies on\ Illuminate\ Contracts\ Foundation\ Application and\ Illuminate\ Routing\ Router, and will handle the dependency public function _ _ construct (Application $app, Router $router) {$this- > app = $app; / / the main delegate $router to handle $this- > router = $router; / the following are middleware settings $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 handling: calling $this- > instances ['app']\ Illuminate\ Routing\ Router directly when make: calling anonymous functions corresponding to concrete in $this- > bindings [' router'] array directly through aliases when make Router dependency\ Illuminate\ Contracts\ Events\ Dispatcher and\ Illuminate\ Container\ Containerpublic function _ construct (Dispatcher $events, Container $container = null) {$this- > events = $events; $this- > routes = new RouteCollection Processing of $this- > container = $container?: new Container;}\ Illuminate\ Contracts\ Events\ Dispatcher: when make, the anonymous function Dispatcher corresponding to concrete in the array $this- > bindings ['events'] is called directly by alias. Dispatcher dependency\ Illuminate\ Contracts\ Container\ Containerpublic function _ construct (ContainerContract $container = null) {$this- > container = $container?: new Container }\ Illuminate\ Container\ Container processing: directly call $this- > instances ['Illuminate\ Container\ Container'] = Object (app)\ Illuminate\ Contracts\ Container\ Container when make: call $this- > instances [' app'] = Object (app) directly when calling an alias when make, but there is no difference

Note: the dependencies listed above are directly entrusted to the service container for automatic processing. There is no need to be afraid.

For the handling of $this- > bindings ['router'] and $this- > bindings [' events'] binding events, the anonymous function corresponding to the array key concrete will be called directly when make.

Code snippet used in 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\ Routerpublic function _ construct (Dispatcher $events, Container $container = null) {$this- > events = $events; $this- > routes = new RouteCollection; $this- > container = $container?: new Container;}

A Router object is returned, and the $this- > instances ['router'] = $router object is reset for direct call next time.

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

A Dispatcher object is returned, and the $this- > instances ['events'] = $dispatcher object is reset for direct call next time.

Note:

Kernel object is an object that combines application and routing, and routing injects IlluminateEventsDispatcher object, which is the core object.

This is the end of the article on "sample Analysis of kernel instantiation in Laravel". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report