AOP? We’ve seen it before.
Recently, in learning Spring, I deeply feel that it is not unreasonable why this framework is so popular. I have been using PHP before. The framework uses codeigniter. Because of its simple and lightweight features, it is very popular, but there are many problems. However, Spring has solved many problems, such as
Some suggestions for CodeIgniter in PHP project, the implementation of login logic mentioned here, is the prototype of Spring Face-Oriented Programming. So as soon as I saw Spring’s AOP, I felt that:We’ve seen it before.。 Essentially, it is the separation of concerns, which can clearly separate different business logic. It not only improves the development efficiency (different logic is written by different people in parallel), but also facilitates the maintenance of code (without looking for logs or validating codes throughout the project).
The method of realizing aspect in CodeIgniter is either to extend CI core class as mentioned above or to use hook. See hook-extension framework core for details.
MVC? Backfeeding Codeigniter
When using the classical M VC mode of Codeigniter to develop, we have enjoyed many conveniences. Business logic (C), data storage (M) and view (C) are separated, so the structure is clear and the idea is natural. But there are also some problems, such as when I write back-end, I usually face two front-end: one is mobile APP, the other is website. These two usually have some overlap of business logic, such as displaying a list of news, usually using two sets of modules, as shown in the figure.
The API is for mobile APP, and his list method is
$this->load->model ( 'api/Box_model','boxm' ); $data = $this->boxm->getRec ( $startCount, $perNumber ); echo_json ( $data );
The web is web-oriented, and his list method is
$this->load->model ( 'web/Box_model','boxm' ); $data= $this->boxm->getRec ( $startCount, $perNumber ); $this->load->view ( 'web/list', $data );
In this way, it is equivalent to the controller writing twice, the model writing twice, and many CtrL + C, CtrL + V. In fact, there are so many.
CVCode, we should know that this architecture is not reasonable, because when the demand changes, we have to change one place, and CV to another place… Spring’s learning shows me the way to solve this problem is to learn to pull out logic, to separate the real business logic and view logic, that is, to divide the three-tier MVC into three layers.View – Controller – Service – RepositoryFour layers, Repository is responsible for data storage, Service is responsible for real business logic, Controller is responsible for user interface related logic, View is responsible for user interaction.
So the above code can be changed to
$this->load->service('list_service'); $data = $this->list_service->getRec ( $startCount, $perNumber ); echo_json ( $data );
$this->load->service('list_service'); $data = $this->list_service->getRec ( $startCount, $perNumber ); $this->load->view ( 'web/list', $data );
This unification allows Service to deal with business logic. Controller needs to deal with different user interfaces with data (one is to return JSON data, the other is to render HTML pages). When the business logic changes, it only needs to modify a service.
So how do you implement this function in Codeigniter? This article makes it clear that the CI framework supports the service layer
Simply put, that’s it.
First of all, define a Service class by yourself. Be careful to add
$this->CI =& get_instance();This ensures that you can load CI resources, such as models, auxiliary functions and libraries, in Service classes
Then modify the CI loader to make the
$this->load->service('list_service');This code is feasible. The code here can be written in imitation of the native loader of CI, such as in
Finally, different services can be invoked in Controller and different models can be invoked in Service.
The design pattern is also advancing with the times. In the last century, MVC pattern was enough for only one kind of terminal on the web. However, when various terminals appeared, MVC would also cause many problems (of course, this is only one reason). So new patterns appeared. So the classical ones need to be learned and updated. We should always think about the problems in the project. Maybe we can develop a design pattern ourselves. Come out.
Welcome to my personal home page mageek (mageek. cn)