Home  >  Article  >  Backend Development  >  php implements MVC, phpmvc_PHP tutorial

php implements MVC, phpmvc_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:08:09767browse

php implements MVC, phpmvc

Using MVC in PHP is becoming more and more popular, especially in some open source frameworks. MVC is sufficient for most situations, but there are some situations where it is not suitable, such as relatively simple personal blogs. For blogs with only a few hundred articles, using MVC feels a bit too complicated; similarly for blogs with only a few hundred articles, MVC is not suitable for most situations. For portal websites such as Sina, using MVC, a large number of files will be loaded, and the impact on speed is unacceptable. Maple Bamboo Dream introduces the basic principles of MVC and a simple implementation. The following introduction is suitable for PHP development.

MVC in PHP

MVC[1] is a software architecture in software engineering. From a PHP perspective, MVC is a little different.

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

View, graphical interface logic. Responsible for output in PHP, handling how to call templates and required resource files.

Controller (controller) is responsible for forwarding requests and processing requests. In PHP, the view to be called and the data to be used are determined based on the request.

Why use MVC

The main function of MVC is to layer and classify code.

The main purpose of MVC is to solve the problem of separating development and design work in Web development, making the work relatively independent.

During this process, we also discovered some other advantages. 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, agree on the URL when requesting the page, and implement it with the following structure:

localhost/index.php?c=demo&a=index&param=welcome

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

As can be seen from the above parameters, the file being accessed is index.php, which also contains three parameters: c, a, and param.

MVC Directory Structure

Next, plan the directory structure of MVC as follows:

001 /*
002 ├─www                       # 网站根目录
003 │  ├─controller             # 控制器目录
004 │  │  ├─democontroller.php  # demo控制器
005 │  ├─model                  # 模型目录
006 │  │  ├─model.php           # model模型
007 │  ├─view                   # 视图目录
008 │  │  ├─index.php           # index视图
009 │  ├─index.php              # 入口文件
010 */

controller

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

001 // controller/democontroller.php
002 class DemoController
003 {
004     public function index()
005     {
006     echo 'hello world';
007     }
008 }// End of the class DemoController
009  
010 // End of file democontroller.php

在这个文件中仅仅定义了一个DemoController的类,且其只包含一个index方法,用于输出hello world

将下面代码添加到入口文件index.php文件中。

001 //index.php
002 require('controller/democontroller.php');
003 $controller new DemoController();
004 $controller->index();
005  
006 // End of index.php

在浏览器中使用上面的约定的URL进行访问,看到输出hello world。当然如果你请求的URL不是那样,而是如下面所示也能得到同样的输出。

localhost/index.php?c=abc

发现URL中的参数还没有任何作用。

如果使用下面的URL进行访问,可以预见不会有任何输出。

localhost/controller/democontroller.php

可以看到要想访问这个网站并得到正确的结果,目前只能通过index.php来访问,这也是为什么称它为入口文件的原因。现在无论参数如何只能访问同样一个页面,那么如何来决定显示不同的结果呢?或者调用不同的控制器呢?

改进入口文件

下面利用URL中的参数来决定使用哪个控制器。

001 //index.php
002 // get runtime controller prefix
003 $c_str $_GET['c'];
004 // the full name of controller
005 $c_name $c_str.'controller';
006 // the path of controller
007 $c_path 'controller/'.$c_name.'.php';
008 // get runtime action
009 $method $_GET['a'];
010 // load controller file
011 require($c_path);
012 // instantiate controller
013 $controller new $c_name;
014 // run the controller  method
015 $controller->$method();
016  
017 // End of index.php

Similarly use the above agreed URL to access in the browser and see the output hello world. Comments in the code explain the purpose of each step. Of course, you can call different controllers and their methods by changing the c and a values ​​in the URL parameters to output different results.

ViewView

We just used the controller controller before, and dynamically called different controllers in the entry file index.php. Then joining the view will show the separation.

001 // view/index.php
002 class Index {
003     public function display($output) {
004         // ob_start();
005         echo $output;
006     }
007 }
008  
009 // End of index.php

视图目录中的index.php文件中定义了Index方法,且只有一个display()函数,负责将传递给它的变量进行输出。
下面修改控制器文件。

001 // controller/democontroller.php
002 class DemoController
003 {
004     private $data 'Hello furzoom!';
005     public function index()
006     {
007     //echo 'hello world';
008     require('view/index.php');
009     $view new Index();
010     $view->display($data);
011     }
012 }// End of the class DemoController
013  
014 // End of file democontroller.php

在控制器中定义了一个data私有变量,index()方法不再直接输出,而是使用视图对象处理输出。此时,按上面的约定的URL进行访问时,将看到输出:

Hello furzoom!

可以根据不同的请求调用不同的视图类,以不同的形式显示数据。这样就将增加了视图的作用,设计人员可以只针对视图进行页面的设计。

模型Model

上面貌似已经很cool了,但是显示什么样的内容是在控制器中直接指定的,希望内容也由URL指定,这样将数据的处理交给模型来处理。

001 // model/model.php
002 class Model {
003     private $data array(
004                 'title' => 'Hello furzoom',
005                 'welcome' => 'Welcome to furzoom.com',
006                 );
007  
008     public function getData($key) {
009         return $this->data[$key];
010     }
011 }
012  
013 // End of model.php

The view file model.php defines a Model class, which defines a getData() method to return the requested data.

Also modify the entry file index.php as follows:

001 //index.php
002 // get runtime controller prefix
003 $c_str $_GET['c'];
004 // the full name of controller
005 $c_name $c_str.'controller';
006 // the path of controller
007 $c_path 'controller/'.$c_name.'.php';
008 // get runtime action
009 $method $_GET['a'];
010 // get runtime parameter
011 $param $_GET['param'];
012 // load controller file
013 require($c_path);
014 // instantiate controller
015 $controller new $c_name;
016 // run the controller  method
017 $controller->$method($param);
018  
019 // End of index.php

Added a parameter $param and used it as a method call parameter of the controller.

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

001 // controller/democontroller.php
002 class DemoController
003 {
004     // private $data = 'Hello furzoom!';
005     function index($param)
006     {
007     // echo 'hello world';
008         require('view/index.php');
009     require('model/model.php');
010     $model new Model();
011     $view new Index();
012     $data $model->getData($param);
013     $view->display($data);
014     }
015 }// End of the class DemoController
016  
017 // End of file democontroller.php

包含需要的视图文件和模型文件,然后生成视图与模型文件,接着通过模型对象取得数据,再用视图对象来输出取得的数据。

此时,在浏览器中使用上面的约定的URL进行访问,将得到输出如下:

Welcome to furzoom.com

如下图:

至此PHP的MVC模式已经基本介绍完成了,剩余的工作就是根据需要进行添加扩充了,很简单吧!!

参考文章

[1] http://zh.wikipedia.org/wiki/MVC

[2] http://blog.chinaunix.net/uid-20761674-id-75075.html

[3] http://wo.zdnet.com.cn/blog-476979-6965.html

[4] http://www.cnblogs.com/cocowool/archive/2009/09/08/1562874.html

转载请注明:http://furzoom.com/php-instantiate-mvc/ By 枫竹梦

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/952775.htmlTechArticlephp实现MVC,phpmvc 在PHP中使用MVC越来越流行了,特别是在一些开源的框架当中。MVC足以应对大多数的情况,但还有一些情况是其不太适合的,...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn