Home >PHP Framework >Laravel >Detailed explanation of the core architecture of the Laravel framework
The following tutorial column will introduce you to the core architecture of the Laravel framework in detail. I hope it will be helpful to friends in need!
Friends who have used the laravel framework know that in addition to providing some basic functions (such as controllers, views, models), there are also intermediate Parts, facades, contracts, etc. How are these things used in the laravel framework? Let me talk to you in detail today.
First of all, you should understand the architectural pattern of the laravel framework (Design core
, the laravel framework is developed using the service component development model, and the laravel framework is composed of different service components)
Multiple service providers in laravel constitute laravel components. Hierarchical design: Put class libraries with the same functions in the same folder. The laravel framework has multiple classes that make up services, and multiple services make up components. Class -> Service -> Component
Laravel uses a component-based development model,
Multiple classes-> Service-> Component, multiple classes form services, multiple services Constituent components.
Multiple components provide different services, and then multiple services constitute our project.
Theoretically, the life cycle mainly has these stages, but among them, Most developers only need to focus on routing, middleware, controllers, closure functions, logic processing, etc.
Of course, there will still be more detailed execution processes inside each step. , here, we generally do not study the framework or transform the framework in depth, and rarely conduct detailed research, but studying the bottom layer is still a good choice for learning.
Service means providing what you need. The services provided in laravel include authentication service, database service, cache service, queue service, etc. All services of the laravel framework are defined in
app/config/app.php
Service provider can provide you with a set of services The thing is the service provider. The server provider actually defined in laravel as shown above, such as
, is the service provider that provides authentication services.
IlluminateCacheCacheServiceProvider::class, a service provider that provides caching servicesBenefits:
Developers can save more energy to deal with project logic, and different development individuals A certain tacit understanding can be achieved between them. The most important thing is that the project achieves hierarchical decoupling. The business logic only depends on the service and does not depend on the underlying implementation of the service. After decoupling, we can upgrade or customize the underlying implementation of the service at will, as long as the underlying class implements the service
Summary: In fact, the service is an abstract concept, the server The provider is the concrete implementer of this abstract concept
Put all services in a box and store the service container. The service container in laravel is located at
vendor/laravel/frameworksrcilluminateContainerContainer.php.
Container.php, which is the service container of the laravel framework.
is used to plan the format, methods, parameters, etc. of the service provider, and regulates certain constraints for the service provider. Therefore, all contracts in the framework are interfaces, so that service providers can be standardized.
Facade
The facade once again demonstrates Laravel’s excellence in design. It makes Laravel more flexible and easy to expand. Then its concept is:
2 It supplements the service access method. Before using a service, you had to obtain an instance of the service and then call the service method. However, using the facade, you can directly The service is called as a static object. 3 Most of the service alias alias in config/app.php use facade
4 Using facade is risky. It is not that the more you use, the better. This is stated in the manual. A small amount of introduction, but the specifics still need to be discovered during development
Laravel framework overall architecture diagram
如上图所示:laravel框架是由多个服务组件构成的 -> 服务提供者(最下面的不同的服务组件)。Foundation
的 Application
用来创建服务提供者,创建好之后保存在Container
的 Container
的服务容器里面,交由他管理,Application
要继承 Container
。
为了约定服务提供者提供的服务,我们定义一个规范,这就是契约。
对于我们的用户(最上面的用户)想使用laravel框架,必须通过控制器来使用(上面的Controller),使用laravel框架主要是使用laravel里面的服务提供者(上面的 new 服务),这样就是最传统的开发模式,和服务器容器没有直接关系,如果laravel这样设计的话,基本上和其他框架一样,没有任何优势。所以一般不怎么做。
由于有契约,契约是服提供者的接口,所以我们也可以直接使用契约,new 服务旁边的黄色线。使用契约用注入的方式,这样使用的不好之处是如果一个方法里面使用多个契约的话,我们就得注入多个契约,这样代码看起来不优雅。
于是laravel里面就出现了门面,门面的出现方便我们优雅的调用服务器提供者的类。由于每个服务提供者的类太长了如:
IlluminateCookieCookieServiceProvider::class,IlluminateDatabaseDatabaseServiceProvider::class,
所以又引出了别名,使用别名之后 简化了我们调用的服务提供者的类。
事件:laravel里面的模型里面的事件,比如用户对数据库操作时做的一个监听。对整个项目运行进行监听,有监听的动作。类似tp5里面的钩子和行为。
中间件:做用户的请求做一定的过滤。
The above is the detailed content of Detailed explanation of the core architecture of the Laravel framework. For more information, please follow other related articles on the PHP Chinese website!