I. What is the problem with the current framework
At present, the main frame Zend Framework, cake and so on have adopted MVC pattern, and implemented the route assignment of URL. For example, Http://www.xxx.com/user/login will map to the Loginaction method in the Usercontroller object, http://www.xxx.com/user/ The register corresponds to the Registeraction method in the Usercontroller object. This is likely to be the case for the corresponding Usercontroller object.
?
Class Usercontroller extends Controller
{
function Loginaction ()
{
Login
}
function Registeraction ()
{}
}
?>
So what are the problems? It's obvious: it contains unnecessary code! For example, there is absolutely no need to include the contents of the Registeraction () method in your Access/user/login. The above code is just a simple example, in general controller corresponds to a small function module, it will have more functional operations, especially in larger projects. Thus, if there are more than 10 methods in a controller, each request contains a lot of redundant code. and in PHP performance promotion is a very important point: try to avoid the inclusion of irrelevant code!
In my recent small project, I used my own Phpbean framework (framework similar to the Zend Framework), and later development found that indeed each controller contained too many action, and later had to consider streaming. But it's not ideal.
Second, the problem is not because of the wrong face
Many people think that "including redundant code is an error in the face of an object", I disagree. As I said in the previous article: The face of the object can achieve the face of all the functions of the process, and do a better job! The key is to use the object of thinking to face the object, rather than face the process of thinking to write the face of the object of the program.
Thirdly, how can we solve this problem?
The key to the solution is to separate the action. How to separate it? First of all, we must understand the role of controller. Controller is the controller, which forwards the request and forwards the HTTP request to the specific action. Note: There is no controller file in struts (note that there is no controller), it is mapped directly to the action file. So we can put the controller directly in the route forwarding, while the real process control, logic processing, etc. put into action.
Let's say the example above, we can separate into two files:
loginaction.php
?
Class Loginaction extends Action
{
function Run ()
{}
}
?>
And
registeraction.php
?
Class Registeraction extends Action
{
function Run ()
{}
}
?>
This enables the separation of action. Registeraction code is not included when you access the/user/login request.
But there are two other problems:
First, the actual project will be very many action files, how effective management is a key.
Second, the same function module in the operation may have common code, how to share?
The first question is a better solution. Put the action of the same module in a subfolder, which means to allow multiple directories. For example, in our code above, we can put loginaction.php and registeraction.php in the user directory. But note that this will increase the problem of routing allocation, how to achieve it depends on the reader to think for themselves.
The second problem is not difficult to solve, the key is to have object-oriented thinking. Here, we can use object inheritance to implement. For example, we can define a user abstract class first.
?
Class User extends Action ()
{
function __contruct ()
{
such as permission checks.
}
}
?>
Then let Loginaction and Registeraction both inherit with user. This will be a good solution.
Iv. Summary
The solution above is just my thoughts for the past few days and may not be perfect yet. Specific applications can be further refined and optimized. For MVC, the framework I always think that in PHP5, objects are more appropriate and more effective than processes (excluding the cost of creating the object itself). For the use of functions to implement the framework, the previous paragraph in pcti Lectures I have tried, I think the idea is similar, but relatively speaking, I prefer the object.
Finally, the above solution refers to some of the ideas of struts in Java. Thank you Here