Laravel framework in addition to providing some basic functions (such as controller, view, model), there are middleware, facade, contract and so on, how these things are used in laravel framework? I’ll talk about it with you today.

You should first understand the architectural patterns of the Laravel framework (the design core, the Laravel framework is developed using the service componentization development pattern, the Laravel framework is composed of different service components)

Laravel components are composed of multiple service providers in Laravel. Hierarchical design: Put libraries with the same functionality in the same folder.

The Laravel framework has multiple classes that make up services, and multiple services that make up components. Class -> Services -> Components

Laravel uses a componentized development pattern of multiple classes -> services -> components, with multiple classes forming services and multiple services forming components.

Multiple components provide different services, and then multiple services make up our project.

Request life cycle

The general process is shown as follows:

Theoretically, there are many phases of the life cycle, but most developers only need to focus on routing, middleware, controllers, closure functions, logic processing, and so on

Of course, inside each step, there will be more detailed execution process. Generally, there is no in-depth study of the framework or transformation of the framework, rarely detailed research, but studying the bottom layer is still a good choice for learning.

service

Laravel provides authentication services, database services, caching services, queue services, and so on. All laravel framework services are defined in app/config/app.php

Service provider

Can give you a set of services is a service provider, laravel as shown above is actually defined inside the server providers, such as IlluminateAuthAuthServiceProvider: : class, provides certification services the service provider. IlluminateCacheCacheServiceProvider: : class, provide caching service service provider

Benefits: Developers can save more effort to deal with the logic of the project, and there is a tacit understanding between different developers. Most importantly, the project is decoupled hierarchically, and the business logic is only dependent on the service, not the underlying implementation of the service.

After decoupling, we can upgrade or customize the underlying implementation of the service at will, as long as we ensure that the underlying class implements the service

Conclusion: In fact, service is an abstract concept, and the server provider is the concrete implementer to complete this abstract concept

Service container

Put all your services in a box, a container for your services. Laravel inside the service container is located in the vendor/laravel frameworksrcilluminateContainerContainer. PHP.

Container.php is the service Container for the Laravel framework.

contract

The format, method, parameters, etc. used to plan the service provider, and provide some constraints for the service provider. So all contracts within the framework are interfaces that regulate service providers.

The facade

The facade once again demonstrates the excellence of Laravel’s design. It allows Laravel to be more flexible and scalable, so its concept is:

  1. Provides developers with static proxies for services in the service container

  2. It complements the way services are accessed. Before using a service you had to get an instance of the service and then invoke its methods, but using a facade allows you to invoke the service directly as a static object.

  3. Most of the service aliases in config/app.php use facades

  4. The use of facades is a risk, not the more the better, it is described in the manual a little bit, but the details need to be discovered in development

Laravel framework architecture diagram

The Laravel framework is made up of multiple service components -> service providers (the different service components at the bottom).

The Foundation Application is used to create the service provider, which is stored in the service Container of the Container and managed by the Container. The Application inherits the Container.

To agree on the services provided by service providers, we define a specification, which is called a contract.

For our users (the top user) who want to use the Laravel framework, they must use it through the Controller (the Controller above). Using the Laravel framework, they mainly use the service provider in Laravel (the new service above), which is the most traditional development mode. There is no direct relationship to the server container, and if laravel is designed this way, it is basically the same as any other framework without any advantage. So you don’t do that much.

Since there is a contract, the contract is the interface of the service provider, we can also use the contract directly, the yellow line next to the new service. The downside of using contract injection is that if multiple contracts are used in a method, we have to inject multiple contracts, which makes the code look unelegant.

In Laravel comes a facade that allows us to elegantly invoke the server provider’s classes. Because each service provider’s class is too long, for example: IlluminateCookieCookieServiceProvider: : class, IlluminateDatabaseDatabaseServiceProvider: : class, so leads to the alias, Using the alias simplifies the class of the service provider we call.

Events: Events in the laravel model, such as a listener when a user is operating on a database. Monitor the entire project run. Similar to the hooks and behavior in TP5.

Middleware: do user requests to do certain filtering.

Pay attention and don’t get lost

All right, everybody, that’s all for this article. All the people here are talented. As I said before, there are many technical points in PHP, because there are too many, it is really difficult to write, you will not read too much after writing, so I have compiled it into PDF and document, if necessary

Click on the code: PHP+ “platform”

As long as you can guarantee your salary to rise a step (constantly updated)

I hope the above content can help you. Many PHPer will encounter some problems and bottlenecks when they are advanced, and they have no sense of direction when writing too many business codes. I have sorted out some information, including but not limited to: Distributed architecture, high scalability, high performance, high concurrency, server performance tuning, TP6, Laravel, YII2, Redis, Swoole, Swoft, Kafka, Mysql optimization, shell scripting, Docker, microservices, Nginx, etc