MVC is becoming increasingly popular in PHP, especially in some open-source frameworks. This article introduces php's simple mvc Framework implementation method. If you are interested in php's simple mvc Framework knowledge, it is becoming increasingly popular to use MVC in PHP, especially in some open-source frameworks.
1. Overview
The full name of MVC is Model View Controller, short for model-view-controller. It is a Model of software design, organize Code by means of separation of business logic, data, and interface display, and integrate the business logic into a component to improve and personalize the custom interface and user interaction, you do not need to rewrite the business logic. MVC is uniquely developed to map traditional input, processing, and output functions in a logical graphical user interface structure.
2. code structure
3. Code Implementation
<? Php // function. php // Controller calls function C ($ name, $ method) {require_once ('libs/Controller /'. $ name. 'controller. class. php '); // $ testController = new testController (); // $ testController-> show (); eval (' $ obj = new '. $ name. 'controller (); $ obj-> '. $ method. '();');} // Model call function M ($ name) {require_once ('libs/Model /'. $ name. 'model. class. php '); eval (' $ obj = new '. $ name. 'model (); '); return $ obj;} // view call function fun Ction V ($ name) {require_once ('libs/View /'. $ name. 'view. class. php '); eval (' $ obj = new '. $ name. 'view (); '); return $ obj;} // filter the invalid value function daddslashes ($ str) {return (! Get_magic_quotes_gpc ())? Addslashes ($ str): $ str ;}?> <? Php // test. php/* Step 1 viewer-> call the controller, send an instruction to it. Step 2 Controller-> select a suitable model based on the instruction. Step 3 model-> obtain the corresponding data based on the Controller command. Step 4 controller-> select the corresponding view based on the instruction. Step 5 View -> display the data obtained in step 3 as you want */require_once ('view/testView. class. php '); require_once ('model/testModel. class. php '); require_once ('controller/testController. class. php '); $ testController = new testController (); $ testController-> show ();?> <? Php // testController. class. php/* controller is used to call the model, call the view, pass the data generated by the model to the view, and display the */class testController {function show () {/* $ testModel = new testModel (); $ data = $ testModel-> get (); $ testView = new testView (); $ testView-> display ($ data ); */$ testModel = M ('test'); $ data = $ testModel-> get (); $ testView = V ('test '); $ testView-> display ($ data) ;}}?> <? Php // testModel. class. php/* model is used to obtain and process data. */class testModel {function get () {return "hello world" ;}}?> <? Php // testView. class. php/* view is used to organize and beautify the obtained data, and finally output */class testView {function display ($ data) to the user terminal) {echo $ data ;}}?>
Running result:
MVC in PHP
MVC [1] is a software architecture in software engineering. From the php perspective, MVC is somewhat different.
Model, the implementation of application functions, and the implementation of program logic. Responsible for data management and data generation in PHP.
View, graphical interface logic. In PHP, it is responsible for outputting and processing how to call templates and required resource files.
The Controller is responsible for forwarding requests and processing requests. In PHP, the call view and data used are determined based on the request.
Why use MVC
MVC is mainly used to stratify and classify code.
The main purpose of MVC is to solve the separate development and design work in Web development so that its work is relatively independent.
In this process, other advantages are also found. the directory structure of the website is clearer, the website is easier to maintain and expand, and modules can be reused.