Php simple implementation of MVC, phpmvc_PHP tutorial

Source: Internet
Author: User
Php implements MVC and phpmvc. Php implements MVC in a simple way. phpmvc is becoming increasingly popular in PHP, especially in some open-source frameworks. MVC is sufficient to deal with most situations, but in some cases it is not suitable for php simple implementation of MVC, phpmvc

MVC is becoming increasingly popular in PHP, especially in some open-source frameworks. MVC is sufficient to deal with most situations, but it is not suitable for some situations, such as simple personal blogs and blogs with only a few hundred articles, using MVC makes people think it is too complicated. using MVC for portal websites such as Sina will load a large number of files, which will not affect the speed. Feng Bameng introduces the basic principle and a simple implementation of MVC. The following content is applicable to PHP development.

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.

MVC implementation

Request URL

First, the URL of the request page is implemented in the following structure:

The code is as follows:


Localhost/index. php? C = demo & a = index & param = welcome

If you want to get a more beautiful URL structure, you can optimize the structure, because the URL structure optimization has little to do with this article, and we will share it later.

From the preceding parameters, we can see that the Accessed file is index. php and contains three parameters: c, a, and param.

MVC directory structure

Then, plan the MVC directory structure as follows:

The code is as follows:


/*
├ ── Www # Website root directory
│ ─ ── Controller # controller Directory
│ ├ ── Democontroller. php # demo controller
│ ─ ── Model # model Directory
│ ├ ── Model. php # model
│ Preview-view # view Directory
│ ─ ── Index. php # index view
│ ─ ── Index. php # entry file
*/

Controller

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

The code is as follows:


// Controller/democontroller. php
Class DemoController
{
Public function index ()
{
Echo 'hello world ';
}
} // End of the class DemoController
// End of file democontroller. php

This file defines only one DemoController class and contains only one index method for outputting hello world.

Add the following code to the index. php file.

The code is as follows:


// Index. php
Require ('Controller/democontroller. php ');
$ Controller = new DemoController ();
$ Controller-> index ();
// End of index. php

Use the aforementioned URL in the browser for access. the output is hello world. Of course, if the requested URL is not the same, but the same output can also be obtained as shown below.

The code is as follows:


Localhost/index. php? C = abc

It is found that the parameters in the URL have no effect.

If the following URL is used for access, no output is expected.

The code is as follows:


Localhost/controller/democontroller. php

We can see that to access this website and get the correct results, we can only access it through index. php, which is why it is called an entry file. Now, no matter how the parameter can only access the same page, how can we decide to display different results? Or call different controllers?

Improved Portal file

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

The code is 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

In the same way, use the aforementioned URL in the browser for access. the output is hello world. The comments in the code already illustrate the purpose of each step. Of course, you can call different controllers and their methods by changing the values of c and a in the URL parameter to output different results.

View

Previously, the controller was used, and different controllers were dynamically called in the index. php file. Add the view to separate the display.

The code is as follows:


// View/index. php
Class Index {
Public function display ($ output ){
// Ob_start ();
Echo $ output;
}
}
// End of index. php

The index. php file in the view directory defines the Index method, and there is only one display () function that outputs the variables passed to it.
Modify the controller file.

The code is 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 private data variable is defined in the controller. The index () method does not directly output data, but uses view objects to process output data. In this case, the output is displayed when you access the service based on the URL specified above:

Hello furzoom!
Different View classes can be called based on different requests to display data in different forms. This will increase the role of the view, the designer can only design the page for the view.

Model

The above seems to be cool, but the content displayed is directly specified in the controller, and you want the content to be specified by the URL, so that the data processing is handed over to the model for processing.

The code is 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, which defines a getData () method to return the requested data.

Modify the index. php file as follows:

The code is 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 call parameter.

You also need to modify the controller method to obtain different data based on different parameters.

The code is 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

Include the required View files and model files, generate the view and model files, obtain data through the model object, and then output the obtained data using the View object.

In this case, the following output is displayed when you use the aforementioned URL in the browser to access the service:

Welcome to furzoom.com
For example:

Now the MVC mode of PHP has been basically introduced, and the rest of the work is to add and expand as needed. it's easy !!

Using MVC in PHP is becoming increasingly popular, especially in some open-source frameworks. MVC is sufficient to deal with most situations, but there are still some situations where it is not suitable...

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.