Partial hierarchical splitting based on yii2 framework
controller
- Simply use the transparent transmission layer as a parameter, re-encapsulate the base class (some authentication, token and other functions are added later)
Split the model layer into three layers: service (service layer), bo (data object/processing layer), dao (persistence layer)
- service: mainly deals with complex business logic. A controller in the module corresponds to a subdirectory of service, and a method corresponds to a class, which can satisfy the independence and cohesion of complex business logic.
- Bo: It is mainly responsible for obtaining data, such as obtaining db data, cache data, external interface data, processing packaging data, and design follows the principle of singleness of the method.
- dao: It is mainly responsible for data persistence. There is no business logic processing code in the operation of db. It is just adding, deleting, modifying and checking operations. It can allow a little data formatting.
Re-encapsulation of configuration classes
- The environment environment that does not use Yii is currently divided into three environments: dev, test, and prod. The three environments are independent. They are directly used to add DEBUG and RUN_MODE in the php.ini in the current running php environment. This is used to decide whether to enable debug mode and to decide which environment configuration file to use.
Re-encapsulation of exception handling classes
- It mainly targets the convergence of exceptions that occur in business logic, and manages exception codes and exception information in a unified manner.
Repackage constant configuration
- Converge the constants scattered in their respective business codes into a constant, and define the constant class according to the business block.
Added SQLmap module
- All SQL statements must be defined in SQLmap and operated with command processing in re-encapsulated db. The biggest advantage of using SQLmap is to facilitate SQL management and auditing and reduce the drag on application performance of complex SQL.
Added cache module
- All cache key statements must be defined in cache, and operate cache in conjunction with the reencapsulation of cache processing in library. The biggest advantage of using SQLmap is to facilitate cache key and time management audits, reduce key conflicts and prevent cache penetration and avalanches.
Added parameter verification module
- According to the business logic, some input business data can be checked, such as the necessity, length, etc., and anonymous function verification can also be passed in.
Added library library
- Mainly some processing tool components
Rewrite response
- Designed completely according to restAPI, all return is unified as json
Added middleware layer
- Perform the required operations for configuration according to the module, such as authentication, filtering, and anti-attack loading of middleware such as on-demand loading
Added message queue/delayed message queue
- Integrate beanstalk as a message queue service, and supports delayed message queues
Repackage Log Tools
- Use messages to process concurrent log writing requirements asynchronously. Production logs can be used in different applications. Multiple processes can be opened at the same time by using a server to process log writing.
Added full text search
- Synchronize mysql based on elasticsearch to do full-text search service
All the above changes do not involve the core library changes of yii2, so yii2 can be upgraded normally in principle. In addition, all core components in yii2 can also be used normally. It is recommended to use them in the redefined level to enable business decoupling and business convergence.
The configuration reference demo.RUN_MODE = dev/test/prod demo.DEBUG = true corresponds to development, testing and online environments respectively. Note that debug is false in prod mode.