php method to implement mvc: first agree on the URL when requesting the page; then plan the directory structure of MVC; then create the controller controller, and use the parameters in the URL to determine the controller to use; finally set the view View and model, and modify the entry file.
Recommended: "PHP Video Tutorial"
The operating environment of this tutorial: Windows 7 system, PHP version 7.1, This method works for all brands of computers.
php implements mvc
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 (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 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
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 accessed file is index.php
, which also contains 3 parameters: c
, a
, param
.
Next, plan the directory structure of MVC as follows:
/* ├─www # 网站根目录 │ ├─controller # 控制器目录 │ │ ├─democontroller.php # demo控制器 │ ├─model # 模型目录 │ │ ├─model.php # model模型 │ ├─view # 视图目录 │ │ ├─index.php # index视图 │ ├─index.php # 入口文件 */
Controller controller
Add the following code Go to the controller/democontroller.php
file.
// controller/democontroller.php class DemoController { public function index() { echo 'hello world'; } }// End of the class DemoController // End of file democontroller.php
Only one class of DemoController
is defined in this file, and it only contains one index
Method, used to output hello world
.
Add the following code to the entry file index.php
file.
1 //index.php 2 require('controller/democontroller.php'); 3 $controller = new DemoController(); 4 $controller->index(); 5 6 // End of index.php
Use the above agreed URL to access in the browser and see the output hello world
. Of course, if the URL you request is not like that, but as shown below, you can get the same output.
localhost/index.php?c=abc
It is found that the parameters in the URL have no effect.
If you use the following URL to access, you can expect that there will be no output.
localhost/controller/democontroller.php
You can see that if you want to access this website and get the correct results, you can currently only access it through index.php
, which is why it is called the entry file. Now you can only access the same page regardless of the parameters, so how do you decide to display different results? Or what about calling a different controller?
The following uses the parameters in the URL to decide which controller to use.
//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 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.
Only used the controller controller before, and dynamically called different controllers in the entry file index.php
. Then joining the view will show the separation.
// 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, which is responsible for outputting the variables passed to it. .
Modify the controller file below.
// 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. The index() method no longer outputs directly, but uses the view object to process the output. At this point, when accessing according to the agreed URL above, you will see the output:
Hello furzoom!
可以根据不同的请求调用不同的视图类,以不同的形式显示数据。这样就将增加了视图的作用,设计人员可以只针对视图进行页面的设计。
上面貌似已经很cool了,但是显示什么样的内容是在控制器中直接指定的,希望内容也由URL指定,这样将数据的处理交给模型来处理。
// 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
视图文件model.php定义了一个Model
类,类中定义了一个getData()
的方法,用于返回请求的数据。
同时修改入口文件index.php如下:
//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
增加了一个参数$param
,将其作为控制器的方法调用参数。
还需要修改控制器的方法根据不同参数取得不同的数据。
// 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
包含需要的视图文件和模型文件,然后生成视图与模型文件,接着通过模型对象取得数据,再用视图对象来输出取得的数据。
此时,在浏览器中使用上面的约定的URL进行访问,将得到输出如下:
Welcome to furzoom.com
至此PHP的MVC模式已经基本介绍完成了,剩余的工作就是根据需要进行添加扩充了,很简单吧!!
The above is the detailed content of How to implement mvc in php. For more information, please follow other related articles on the PHP Chinese website!