The use of MVC in PHP has become increasingly popular, especially in some open-source frameworks. MVC is good enough for most situations, but there are some situations where it's less appropriate, like a simpler personal blog, where using MVC is a bit too complicated for blogs with only hundreds of posts, and also for portals like Sina, which use MVC, will have a lot of files loaded, The impact on speed is unacceptable. Maple Bamboo Dream Introduction to the basic principles of MVC and a simple implementation. The following introductory content is applicable to PHP development.
MVC in PHP
MVC[1] is a software architecture in software engineering. There are some differences in MVC from the point of view of PHP.
Model (models), the implementation of program application functions, the implementation of the logic of the program. Responsible for data management and data generation in PHP.
View, graphical interface logic. Responsible for the output in PHP, processing how to invoke the template, the required resource files.
Controller, which is responsible for forwarding the request and processing the request. The views and data used in PHP are determined on request.
Why use MVC
The main purpose of MVC is to layer and categorize code.
The main purpose of MVC is to solve the separation development and design work in web development, and make it work relatively independent.
In this process also found some other advantages, the site's directory structure is more clear, the site is easier to maintain and expand, you can achieve the reuse of modules.
MVC implementation
Request URL
First, the URL of the request page is agreed upon, and is implemented in the following structure:
Copy the code code as follows:
Localhost/index.php?c=demo&a=index¶m=welcome
If you want to get a more graceful URL structure, you can optimize, for this URL structure optimization is not related to this article, and later to share.
From the above parameters can be seen, access to the file is index.php, and also contains 3 parameters are C, a, param.
MVC directory Structure
Next, the directory structure of the plan MVC is as follows:
Copy the code code as follows:
/*
├─www # Web site root directory
│├─controller # Controller Directory
││├─democontroller.php # Demo Controller
│├─model # Model Catalog
││├─model.php # Model Models
│├─view # View Directory
││├─index.php # Index View
│├─index.php # Portal File
*/
Controller controllers
Add the following code to the controller/democontroller.php file.
Copy the code code as follows:
controller/democontroller.php
Class Democontroller
{
Public Function Index ()
{
echo ' Hello World ';
}
}//End of the class Democontroller
End of File democontroller.php
In this file, only one Democontroller class is defined, and it contains only one index method, which is used to output Hello world.
Add the following code to the portal file index.php file.
Copy the code code as follows:
index.php
Require (' controller/democontroller.php ');
$controller = new Democontroller ();
$controller->index ();
End of index.php
Use the URL of the convention above in the browser to access and see the output Hello World. Of course, if the URL you requested is not, you can get the same output as shown below.
Copy the code code as follows:
Localhost/index.php?c=abc
The parameters in the discovery URL do not have any effect.
If you use the following URL for access, you can foresee that there will be no output.
Copy the code code as follows:
localhost/controller/democontroller.php
You can see that you want to access this site and get the correct results, which is now only accessible through index.php, which is why it is called a portal file. Now, no matter how the parameter can only access the same page, then how to decide to display different results? or call a different controller?
Improved entry file
The following parameters are used in the URL to determine which controller to use.
Copy the code code as follows:
index.php
Get Runtime controller Prefix
$c _str = $_get[' C ');
The full name of the controller
$c _name = $c _str. ' Controller ';
The path of controller
$c _path = ' controller/'. $c _name. PHP ';
Get Runtime action
$method = $_get[' a '];
Load Controller file
Require ($c _path);
Instantiate controller
$controller = new $c _name;
Run the Controller method
$controller $method ();
End of index.php
Also in the browser using the above Convention URL to access, see Output Hello World. The comments in the code have explained the purpose of each step. Of course, you can invoke different controllers and their methods by changing the C and a values in the URL parameters to output different results.
Views View
The controller controllers are used in the front, and dynamic calls to different controllers are implemented in the portal file index.php. Then join the view to show the detach.
Copy the code code as follows:
view/index.php
Class Index {
Public function display ($output) {
Ob_start ();
Echo $output;
}
}
End of index.php
The index method is defined in the index.php file in the view directory, and there is only one display () function that is responsible for outputting the variables passed to it.
Modify the controller file below.
Copy the code code as follows:
controller/democontroller.php
Class Democontroller
{
Private $data = ' Hello furzoom! ';
Public Function Index ()
{
echo ' Hello World ';
Require (' view/index.php ');
$view = new Index ();
$view->display ($data);
}
}//End of the class Democontroller
End of File democontroller.php
A data private variable is defined in the controller, and the index () method no longer outputs directly, but instead uses the View object to process the output. At this point, when accessed by the URL of the convention above, you will see the output:
Hello furzoom!
Different view classes can be called according to different requests, displaying the data in different forms. This increases the effect of the view, which allows the designer to design the page only for the view.
Model
It seems to be cool, but it shows what kind of content is directly specified in the controller, the hope content is also specified by the URL, so that the processing of data to the model to handle.
Copy the code code as follows:
model/model.php
Class Model {
Private $data = Array (
' Title ' = ' Hello furzoom ',
' Welcome ' = ' Welcome to furzoom.com ',
);
Public Function GetData ($key) {
return $this->data[$key];
}
}
End of model.php
View File model.php defines a model class in which a GetData () method is defined to return the requested data.
Also modify the entry file index.php as follows:
Copy the code code as follows:
index.php
Get Runtime controller Prefix
$c _str = $_get[' C ');
The full name of the controller
$c _name = $c _str. ' Controller ';
The path of controller
$c _path = ' controller/'. $c _name. PHP ';
Get Runtime action
$method = $_get[' a '];
Get runtime parameter
$param = $_get[' param ');
Load Controller file
Require ($c _path);
Instantiate controller
$controller = new $c _name;
Run the Controller method
$controller-$method ($param);
End of index.php
A parameter $param is added as a method invocation parameter of the controller.
It is also necessary to modify the controller's methods to obtain different data according to different parameters.
Copy the code code as follows:
controller/democontroller.php
Class Democontroller
{
Private $data = ' Hello furzoom! ';
function Index ($PARAM)
{
echo ' Hello World ';
Require (' view/index.php ');
Require (' model/model.php ');
$model = new Model ();
$view = new Index ();
$data = $model->getdata ($param);
$view->display ($data);
}
}//End of the class Democontroller
End of File democontroller.php
Contains the required view files and model files, then generates the view and model files, then obtains the data through the model object, and then uses the View object to output the obtained data.
At this point, in the browser using the above Convention URL to access, will be output as follows:
Welcome to Furzoom.com
Such as:
Now the PHP MVC pattern has been basically introduced, the rest of the work is based on the need to add expansion, very simple!!
Source: https://www.cnblogs.com/foonsun/p/5788564.html
PHP Simple Implementation MVC