Home  >  Article  >  Backend Development  >  PHP之简单实现MVC框架,phpmvc框架_PHP教程

PHP之简单实现MVC框架,phpmvc框架_PHP教程

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

PHP之简单实现MVC框架,phpmvc框架

1.概述

  MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种业务逻辑、数据、界面显示分离的方法组织代码,将业务逻辑聚集到一个部件里面,在改进和个性化定制界面及用户交互的同时,不需要重新编写业务逻辑。MVC被独特的发展起来用于映射传统的输入、处理和输出功能在一个逻辑的图形化用户界面的结构中。

2.代码结构

3.代码实现

<?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;
		}
	}
?>

 运行结果:

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1076313.htmlTechArticlePHP之简单实现MVC框架,phpmvc框架 1.概述 MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,...
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