The simplest way to implement MVC development in PHP is through a single point of entry.

Source: Internet
Author: User

Today, I began to write in detail my understanding of MVC and a process for implementing my own framework during the development process. Which may be incorrect. I hope to correct it!

There are a lot of teaching materials on the website about MVC. I don't want to talk about it here. I don't understand it. I recommend that you search for "MVC" over hundreds of times. I believe you can find what you want. Here I will talk about my understanding of MVC in PHP development. M, module, the main task is to read the data of the database or other file systems as needed. V, view, my understanding is mainly responsible for the page first, the data is displayed as HTML to the user. C and controller are mainly responsible for the business logic. For example, to display the login interface, you need to call the loginaction method of the controller usercontroller to display it (maybe this does not need to call the module ), for example, if you perform a login check, you can call the usercontroller method checkaction for logical processing.

Reference

M Refers to the model. The controller is not involved in the business logic. The entire business logic should be placed in the model layer. The Controller only serves to distribute requests. That is to say, to obtain the current request, the Controller decides which model to call to obtain data, and then assigns a value to which view to render the page.

For specific implementation of MVC, I think it is best to achieve a single point of entry.

What is a single point of access? The so-called single point of entry means that the entire application has only one entry, and all implementations are forwarded through this entry. For example, we use index above. PHP is the single point of entry for the program. Of course, this can be controlled by yourself.

Why is single point of access required? The single point entry has several benefits: first, some variables, classes, and Methods processed globally by the system can be processed here. For example, If You Want To preliminarily filter data and simulate session processing, you need to define some global variables, you even need to register some objects or variables in the register. (This is mainly implemented globally. For details, refer to some articles I translated earlier, "Use global variables in PHP".) Second, the program architecture is clearer and clearer. Of course, there are many advantages. I will not list them one by one. You can try it out.

Of course, the single point portal also has some shortcomings. For example, when your system is large, it is impossible to use only one point as the portal, especially when the system has two completely unrelated functions, but I'm glad to say that, this is extensible. You can expand multiple portals. For example, in a recent big project, the background management is irrelevant to the foreground, so I have two portals: index. php and Admin. php. This is not to say that the single point of access is not good (you can try it out ).

So, how to implement a single point of entry? This is my focus in this section. Generally, it is implemented through URL address ing (I mentioned in the previous article: answer several questions in phpchina: URL ing, which implements the core of a single point of entry, here I will implement it and demonstrate it ). The most important thing about a single point of entry is to pass parameters in a URL to allocate a program. Specifically, for example, the address is index. php? Controller = Test & Action = test, which uses index. PHP forwards the request to the corresponding testcontroller file and executes the corresponding testaction method (the controller and method naming here refer to Zend
Framework idea ).

The simplest method is to implement the single point of entry mentioned above (remember: For simplicity, I have not used URL ing here)

/Index. php

<?

/**

* MVC demo

* Only implement the most basic MVC functions, excluding security processing, data filtering, and other optimization measures.

*/

Define ('site _ path', str_replace (", '/', dirname (_ file _); // define the system directory

$ Controller = (! Empty ($ _ Get ['controller'])? $ _ Get ['controller']: 'index'; // gets the controller. The default Index

$ Action = (! Empty ($ _ Get ['action'])? $ _ Get ['action']: 'index'; // method name, default Index

$ Controller_name = $ controller. 'controller ';

$ Controller_file = site_path. '/APP/controller/'. $ controller_name. '. Class. php'; // obtain the Controller File

If (file_exists ($ controller_file )){

Require_once ($ controller_file );

$ Controller = new $ controller_name ();

$ Controller-> {$ action. 'action '}();

} Else {

Die ('the corresponding controller cannot be found! ');

}

?>

Corresponding demo

/APP/controller/testcontroller. Class. php (note the path)

<?

/**

* MVC demo

* Only implement the most basic MVC functions, excluding security processing, data filtering, and other optimization measures.

*/

Class testcontroller

{

Function testaction (){

Echo 'hello, world! ';

}

}

?>

Open the browser and enter http: // path/to/yoursite/index. php? Controller = Test & Action = test (change your path accordingly). If you see hello, world! The first step of MVC is that the single point of access is successful!

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.