Incrementally improve the performance of your PHP framework

Source: Internet
Author: User
Tags http request php framework zend zend framework

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. Project address: http://www.songjin.net:8080.

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.