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¶m=welcome
Copy after login
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:
controller
Add the following code to the controller/democontroller.php
file.
004 |
public function index() |
在这个文件中仅仅定义了一个DemoController
的类,且其只包含一个index
方法,用于输出hello world
。
将下面代码添加到入口文件index.php
文件中。
002 |
require ( 'controller/democontroller.php' ); |
003 |
$controller = new DemoController(); |
004 |
$controller ->index(); |
在浏览器中使用上面的约定的URL进行访问,看到输出hello world
。当然如果你请求的URL不是那样,而是如下面所示也能得到同样的输出。
localhost/index.php?c=abc
Copy after login
发现URL中的参数还没有任何作用。
如果使用下面的URL进行访问,可以预见不会有任何输出。
localhost/controller/democontroller.php
Copy after login
可以看到要想访问这个网站并得到正确的结果,目前只能通过index.php
来访问,这也是为什么称它为入口文件的原因。现在无论参数如何只能访问同样一个页面,那么如何来决定显示不同的结果呢?或者调用不同的控制器呢?
改进入口文件
下面利用URL中的参数来决定使用哪个控制器。
005 |
$c_name = $c_str . 'controller' ; |
007 |
$c_path = 'controller/' . $c_name . '.php' ; |
009 |
$method = $_GET [ 'a' ]; |
013 |
$controller = new $c_name ; |
015 |
$controller -> $method (); |
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.
003 |
public function display( $output ) { |
视图目录中的index.php文件中定义了Index
方法,且只有一个display()
函数,负责将传递给它的变量进行输出。
下面修改控制器文件。
004 |
private $data = 'Hello furzoom!' ; |
005 |
public function index() |
008 |
require ( 'view/index.php' ); |
010 |
$view ->display( $data ); |
在控制器中定义了一个data
私有变量,index()方法不再直接输出,而是使用视图对象处理输出。此时,按上面的约定的URL进行访问时,将看到输出:
Hello furzoom!
Copy after login
可以根据不同的请求调用不同的视图类,以不同的形式显示数据。这样就将增加了视图的作用,设计人员可以只针对视图进行页面的设计。
模型Model
上面貌似已经很cool了,但是显示什么样的内容是在控制器中直接指定的,希望内容也由URL指定,这样将数据的处理交给模型来处理。
003 |
private $data = array ( |
004 |
'title' => 'Hello furzoom' , |
005 |
'welcome' => 'Welcome to furzoom.com' , |
008 |
public function getData( $key ) { |
009 |
return $this ->data[ $key ]; |
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:
005 |
$c_name = $c_str . 'controller' ; |
007 |
$c_path = 'controller/' . $c_name . '.php' ; |
009 |
$method = $_GET [ 'a' ]; |
011 |
$param = $_GET [ 'param' ]; |
015 |
$controller = new $c_name ; |
017 |
$controller -> $method ( $param ); |
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.
005 |
function index( $param ) |
008 |
require ( 'view/index.php' ); |
009 |
require ( 'model/model.php' ); |
010 |
$model = new Model(); |
012 |
$data = $model ->getData( $param ); |
013 |
$view ->display( $data ); |
包含需要的视图文件和模型文件,然后生成视图与模型文件,接着通过模型对象取得数据,再用视图对象来输出取得的数据。
此时,在浏览器中使用上面的约定的URL进行访问,将得到输出如下:
Welcome to furzoom.com
Copy after login
如下图:
至此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 枫竹梦
http://www.bkjia.com/PHPjc/952775.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/952775.htmlTechArticlephp实现MVC,phpmvc 在PHP中使用MVC越来越流行了,特别是在一些开源的框架当中。MVC足以应对大多数的情况,但还有一些情况是其不太适合的,...