We learn technology alone, rather than a short communication, you can learn from others all kinds of learning experience, learning methods and learning skills. Therefore, learning and communication need a circle, to improve your learning skills, please click add Technology group: click to enter the code: PHP+ “platform”

Laravel was originally designed for the MVC architecture, which can meet various needs such as event handling, user authentication, and so on. It also has a package manager for managing modularity and extensibility code, powered by an administrative database.

Laravel has won a lot of attention for its simple and elegant features. Both experts and novices will first think of Laravel when developing PHP projects.

In this article we will examine the core architecture of the Laravel framework. The main contents include: container, application framework, kernel, contract, entry file, service provider, routing, facade.

Architecture pattern of Laravel framework (design core, Laravel framework is developed using service componentization development pattern, 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.

Container: a tool that performs dependency injection and manages dependencies

  • Bind objects, instances (shared instances), interfaces (interfaces to implementations), and closures into the container array.

  • Binding abstractions (resolving dependency injection in concrete classes via reflection), i.e. creating instances.

The whole frame provides dependencies around the container, so it’s equivalent to a heart, and a heart for Laravel.

The application

Register the base binding and register the base service in the constructor

  • Constructor [register base binding] to container shared instance array: bind itself to app abstraction; Bind itself to abstraction from the container class

  • Constructor [register basic service] to container binding array: including but not limited to: routing object, request object, log contract interface to file log implementation class, file log service object, mysql log service object…

The kernel

  • Kernel classes inject dependencies (application frameworks (containers)) through constructors

  • The constructor resolves the routing object into the kernel’s properties

  • Pass the request object into the request distribution method of the routing object that resolves the routing object into the kernel’s properties in the constructor

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. The service container inside Laravel is located

vendor/laravel/frameworksrcilluminateContainerContainer.php.
Copy the code

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

Entrance to the file

  • Instantiate the application framework.

  • Route configuration is performed through static methods of the route facade class. Check the corresponding content of the facade for specific execution process.

  • Bind the kernel contract interface to the implementation.

  • Parsing the kernel contract object interface (that is, getting instantiated objects of concrete implementation classes that implement the contract interface).

  • A method that calls the request object to set the request address to the properties of the request object and returns the request object.

  • Pass the request object into the kernel’s request processing method.

Route objects

  • Adds a routing configuration item to the routing table of a routing object

  • The request distribution method performs a lookup of the routing table

  • Perform the route matching to the request, either executing the method under the controller or executing the closure immediately.

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