PHP Simple Implementation mvc_php Instance

Source: Internet
Author: User

Using MVC in PHP has become increasingly popular, especially in some open source frameworks. MVC is enough to deal with most situations, but there are some things that are not appropriate, such as simple personal blog, for only hundreds of article level blog, the use of MVC makes people feel a bit too complex; similarly for Sina and other portals, using MVC, there will be a large number of files to be loaded, The effect on speed is unacceptable. Maple Bamboo Dream Introduction to the basic principles of MVC and a simple implementation. The following description of the application of PHP development.

MVC in PHP

MVC[1] is a software architecture in software engineering. There are some differences in MVC from a PHP perspective.

Model, the implementation of program application functions, the realization of the logic of the program. Responsible for data management and data generation in PHP.

View (views), graphical interface logic. Responsible for output in PHP, dealing with how to call the template, the required resource files.

Controller (Controller), responsible for forwarding requests, processing requests. In PHP, the calling view and the data used are determined according to the request.

Why use MVC

The main role of MVC is to tier and categorize code.

The main purpose of MVC is to solve the separation development and design work in web development and make its work relatively independent.

In this process also found some other advantages, the site's directory structure more clearly, the site easier to maintain and expand, you can achieve the reuse of modules.

MVC implementation

Request URL

First, the URL to the request page is agreed upon to implement the following structure:

Copy Code code as follows:

Localhost/index.php?c=demo&a=index&param=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, later to share.

From the above parameters can be seen, access to the file is index.php, also contains 3 parameters are C, a, param.

MVC directory Structure

Next, the directory structure for planning MVC is as follows:

Copy Code code as follows:

/*
├─WWW # site Root directory
│├─controller # Controller Directory
││├─democontroller.php # Demo Controller
│├─model # Model Directory
││├─model.php # Model
│├─view # View Directory
││├─index.php # Index View
│├─index.php # Entry File
*/

Controller Controller

Add the following code to the controller/democontroller.php file.

Copy 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

Only one Democontroller class is defined in this file, and it contains only one index method for outputting Hello world.

Add the following code to the entry file index.php file.

Copy Code code as follows:

index.php
Require (' controller/democontroller.php ');
$controller = new Democontroller ();
$controller->index ();
End of index.php

In the browser using the above Convention URL to access, see Output Hello World. Of course, if the URL you requested is not like that, you can get the same output as shown below.

Copy Code code as follows:

Localhost/index.php?c=abc

The discovery of parameters in the URL has no effect.

If you use the following URL for access, you can foresee no output.

Copy Code code as follows:

localhost/controller/democontroller.php

You can see that to access this site and get the correct results, currently only through the index.php to access, which is why it is called the Portal file reason. Now regardless of how the parameters can only access the same page, how do you decide to display different results? Or do you call a different controller?

Improving Portal Files

The following uses the parameters in the URL to determine which controller to use.

Copy Code code as follows:

index.php
Get Runtime controller Prefix
$c _str = $_get[' C '];
The full name of 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 call different controller and their methods by changing the C and a values in the URL parameters to output different results.

Views View

Only the controller controller is used in the front, and the dynamic invocation of different controllers is implemented in the portal file index.php. Then join the view to show the detach.

Copy 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 variable passed to it.
The controller file is modified below.

Copy 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, the output is seen when accessed by the URL of the convention above:

Hello furzoom!
You can invoke different view classes to display the data in different forms, depending on the request. This will increase the role of the view, and designers can design the page only for the view.

Model Models

It seems to have been very cool, but the display of what content is directly specified in the controller, I hope that the content is also specified by the URL, so that the processing of data to the model to deal with.

Copy 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

The 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 Code code as follows:

index.php
Get Runtime controller Prefix
$c _str = $_get[' C '];
The full name of 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

Added a parameter $param as the controller's method invocation parameter.

You also need to modify the Controller method to obtain different data according to different parameters.

Copy 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 view files and model files that you want, then generates the view and model files, then takes the data from the model object, and then prints the resulting data with the View object.

At this point, the URL used in the above convention is accessed in the browser, and the output is as follows:

Welcome to Furzoom.com
The following figure:

So far the MVC pattern of PHP has been basically completed, the rest of the work is added according to the need to expand, very simple!!

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.