Yaf-based modular development solution as our website traffic increases, more and more users and more complex requirements, one-stop development is far from supporting our business development needs, during the product development period, more and more problems are encountered, and the technical architecture limits the business development. this is intolerable for R & D personnel, and business constraints are acceptable for technical development, however, technology restricts the development of the business.
Let me summarize the problems that one-stop development will face:
1. a large amount of code, development, deployment, and testing will be a problem
This is easy to understand. when all our business needs are in the same system, there will inevitably be great risks in development, deployment, and testing, such as development, even members who have participated in the project from the very beginning, it is difficult to control risks when the business scale is large. for example, deployment, because of the large volume, the code release is very slow, most of the release platforms will first be packaged and decompressed to the online server. in this process, people will be in urgent need of urgent release. if there is a problem with the launch, the rollback will be higher, the impact is not only on the publishing system, but also on the service unavailability.
2. seriously restricting business development
There is a good habit that developers will habitually encapsulate common functions into classes or methods during the development process. if this is a core function, it may appear anywhere in the entire system, when core functions need to be developed, the system may be restructured.
For example.
For example, we have a function (or a class)
function custom_function($a,$b) {//TODO}
The most basic business function is implemented. at the beginning, it satisfies the current business and implements basic functions. However, as the business develops, this basic function becomes more powerful, more features are supported. to support services, we make function extensions compatible with parameters, such:
Function custom_function ($ a, $ B, $ c) {if (empty ($ c) {// compatible with old businesses} else if ($ c = "XXX ") {// support new business} else if ($ c = "ccc") {// New Business }}
Although it can meet the requirements, it can be imagined that maintaining such a piece of code will lead people to swear? Of course, although we can't hear it, it makes future generations very uncomfortable.
3. push down and rebuild as a last resort
Based on various pressures, we need to reconstruct the entire system as a result of the pressure of business development. this process is long and painful. On the one hand, we need to support the business of the existing system, on the other hand, we also need to rebuild the entire system to reduce coupling between them. this cost and risk will make us feel bad.
Should we initially consider splitting the project into multiple modules from the business? The answer is yes. But it will also cause some problems:
1. PM does not understand the technology and how to split
This problem has been solved. during planning and project review, we discuss it with the technology, focusing on splitting business functions. for example, all user-related operations are classified as user modules, the background is classified as the background module, and the browsing is classified as the view module.
2. how to control the granularity. if the granularity is not well controlled, it will increase more costs.
This requires communication and planning within the technology to control the boundaries.
3. data sharing?
Although it has been split into multiple modules, the data is distributed across the entire system. how do we share data between modules?
In the initial stage of the project,
-Independent basic data into the library module, which provides data sharing with the finest units, which can be called throughout the module;
-Provides cross-module calls for business interfaces. for example, in module A, the framework supports calling interfaces in module B;
-The http service interface is provided, which is usually done when the project is mature;
The framework provides the following features:
1. less code and simplified
2. it can be called across modules. the module almost does not contain any framework implementation.
3. call the most basic library module
4. configuration management
Among all the current php frameworks, it is still difficult to implement modular programming. most of the frameworks implement their own applications or modules within the framework, here is the specific modular business code.
There is also a completely modular architecture in which every module business has the following framework implementation:
However, this disadvantage is obvious. On the one hand, it is difficult to implement cross-module calls. on the other hand, it is necessary to implement logic at the outer layer of the package. on the other hand, framework maintenance and code volume make the module appear heavier.
Therefore, the first method is adopted. Today's topic yaf is actually such an architecture, but the framework logic is implemented in the so extension,
However, what we need to talk about today is based on the methods in the two solutions, which can make the modules completely independent and allow them to call interfaces across modules. In addition, the module basically does not need to maintain any framework code, you only need to write the code according to the framework specification.
Each module provides an interface for other modules to directly call. each module only processes its own business. when there is a need to share the business, you can directly write the interface according to the specifications.
The project directory is as follows:
However, yaf does not support cross-module calls. Modules cannot communicate with each other through interfaces. to solve this problem, I have added two classes: Yaf_Init and Yaf_Caller.
Yaf_Init
$app = Yaf_Init::init();$app->Bootstrap()->run();
This is only a layer of encapsulation. The current module name is parsed according to the Directory, saving the configuration of config. for the sake of flexibility, Yaf_init: init () will be followed by parameters that can be flexibly customized, each webroot/module/index. php contains the above code.
Equivalent to the following:
$ Config = array ('application' => array ('directory '=> app directory) $ application = new Yaf_Application ($ config); $ application-> Bootstrap () -> run ();
To locate the app directory, you also need to add a configuration in php. ini,
Yaf. app_path =/home/admin/web/htdocs/app/
If/home/admin/web/webserver/webroot/module/index. php is accessed,
The module is parsed into the app directory:
/Home/admin/web/htdocs/app/module/to $ config. application. directory,
Pass $ config configuration to the yaf_application constructor for initialization
Yaf_Caller
$Object = new Yaf_Caller('moduleName','className');$Object->test();
This class is used to instantiate the class library of interfaces in other modules. In fact, it is also a simple package, saving the environment variables, switching to a module, and then calling Yaf_loader: autoload (), the constructor receives two parameters. The first parameter is the module name and the second parameter is the interface class name,
Interface naming rules:
/App/htdocs/moduleA/Interface/Admin. php
For example, module A provides an interface with the class name admin
Then, the Admin. php class is written.
class Interface_Admin{ function test(){ //TODO }}
When other modules are called
// The object returns the Interface_Admin class $ object = new Yaf_Caller ('lelea', 'admin'); it can directly call the public method under Interface_Admin. $ Object-> test ();
This solution has the following advantages:
-The module has a small amount of code. you only need to implement the business code.
-The inteface interface can be seamlessly called between modules.
To be continued ......
Source: