Home  >  Article  >  Backend Development  >  PHP - MVC pattern explanation and examples

PHP - MVC pattern explanation and examples

王林
王林forward
2019-08-23 17:31:019773browse

1. MVC pattern flow chart

PHP - MVC pattern explanation and examples

##2. MVC concept

(1) Function

MVC includes Controller, Model, and View.

The function of the controller is to call the model and the view, pass the data generated by the model to the view, and let the view display it

The function of the model is to obtain the data and process the returned data

The function of the view is to beautify the obtained data and output it to the user terminal

(2) Execution process

1. Viewer-> Call the controller and issue instructions

2. Controller-> Select the appropriate model according to the command

3. Model-> Get data according to the command

4. Controller-> Select the view according to the command

5 . View -> Display the obtained data

3. Simple MVC example

(1) Directory planning

PHP - MVC pattern explanation and examples

(2) Writing class files

1. testController.class.php Controller class file

Naming rules :test (name)Controller (controller file).class.php (class file)


  
 get();//模型按照指令取数据
            //按指令选择视图 实例化一个view的对象
            $testView  = new testView();
            //把取到的数据按用户的样子显示出来
            $testView -> display($data);
        }
    }
?>

2. testModel.class.php Model class file

Naming rules: test (model file name) Model ( Model file).class.php Class file

3. testView.class.php View class file

4. Single entry file

Let him call the controller , and the controller calls the model and view

show();//调用方法
?>

5. Running results

PHP - MVC pattern explanation and examples

4. Simple MVC instance improvement----Method Encapsulation

1. Encapsulate an object that instantiates a controller, etc. and a function that calls a method

show();
        eval('$obj = new '.$name.'Controller();$obj->'.$method.'();');//把字符串转换为可执行的php语句
    }
    //封装一个实例化模型的对象和调用方法的函数
    function M($name){
        require_once('/libs/Model/'.$name.'Model.class.php');
        //$testModel = new testModel();
        eval('$obj = new '.$name.'Model();');//实例化
        return $obj;
    }
 
    //封装一个实例化视图的对象和调用方法的函数
    function V($name){
        require_once('/libs/View/'.$name.'View.class.php');
            //$testView  = new testView();
            eval('$obj = new '.$name.'View();');
            return $obj;
    }
 
    //为了安全性 ,过滤函数
    //addslashes对’,字符进行转义
    //get_magic_quotes_gpc()当前魔法符号的打开状态,打开返回true,
    function daddslashes($str){
        return (!get_magic_quotes_gpc() )? addslashes($str) : $str;
    }
?>

2. Rewrite the entry file index.php

Browser URL access form http://...index.php?controller=controller name&method=method name

3. Running results

Browser access http:// localhost:8080/MVC/index.php?controller=test&method=show Show hello world

PHP - MVC pattern explanation and examples

If you want to know more about PHP related issues, please visit the PHP Chinese website:

PHP Video tutorial

The above is the detailed content of PHP - MVC pattern explanation and examples. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete