Home  >  Article  >  Backend Development  >  Simple implementation of MVC framework in PHP, phpmvc framework_PHP tutorial

Simple implementation of MVC framework in PHP, phpmvc framework_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 09:03:57955browse

Simple implementation of MVC framework in PHP, phpmvc framework

1. Overview

The full name of MVC is Model View Controller, which is the abbreviation of model-view-controller. It is a software design model organized by a method of separating business logic, data and interface display. Code, which gathers business logic into a component, improves and personalizes the interface and user interaction without rewriting the business logic. MVC was uniquely developed to map traditional input, processing, and output functionality into a logical graphical user interface structure.

2. Code structure

3. Code implementation

<?php
        //function.php  
	//控制器调用函数
	function C($name, $method){
		require_once('libs/Controller/'.$name.'Controller.class.php');
		//$testController = new testController();
		//$testController->show();
		eval('$obj = new '.$name.'Controller(); $obj->'.$method.'();');
	}

	//模型调用函数
	function M($name){
		require_once('libs/Model/'.$name.'Model.class.php');
		eval('$obj = new '.$name.'Model();');
		return $obj;
	}

	//视图调用函数
	function V($name){
		require_once('libs/View/'.$name.'View.class.php');
		eval('$obj = new '.$name.'View();');
		return $obj;
	}

	//过滤非法值
	function daddslashes($str){
		return (!get_magic_quotes_gpc())?addslashes($str):$str;
	}
?>

<?php
//test.php
/*
第一步 浏览者 -> 调用控制器,对它发出指令
第二步 控制器 -> 按指令选取一个合适的模型
第三步 模型 -> 按控制器指令取相应数据
第四步 控制器 -> 按指令选取相应视图
第五步 视图 -> 把第三步取到的数据按用户想要的样子显示出来
*/

require_once('View/testView.class.php');
require_once('Model/testModel.class.php');
require_once('Controller/testController.class.php');

$testController = new testController();
$testController->show();
?>

<?php
//testController.class.php
/*
控制器的作用是调用模型,并调用视图,将模型产生的数据传递给视图,并让相关视图去显示
*/
	class testController{
		function show(){
			/*$testModel = new testModel();
			$data = $testModel->get();
			$testView = new testView();
			$testView->display($data);*/
			$testModel = M('test');
			$data = $testModel->get();
			$testView = V('test');
			$testView->display($data);
		}
	}
?>

<?php
//testModel.class.php
/*
模型的作用是获取数据并处理,返回数据
*/
	class testModel{
		function get(){
			return "hello world";
		}
	}
?>

<?php
//testView.class.php
/*
视图的作用是将获得的数据进行组织,美化等,并最终向用户终端输出
*/
	class testView{
		function display($data){
			echo $data;
		}
	}
?>

Running results:

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1076313.htmlTechArticlePHP’s simple implementation of MVC framework, phpmvc framework 1. Overview The full name of MVC is Model View Controller, which is a model ) - view - the abbreviation of controller, a software design model,...
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