简单的mvc模式实例
Jun 06, 2016 pm 07:37 PMLynda MVC模式有很多优势,可实现单点控制,分离代码,快速开发,后期容易维护与扩展,代码架构清晰等等。 无 ?php// mvc pattern 单点控制// 简单的控制器 Cclass NumberController extends DefaultController{public $model = null;public function __const
Lynda
MVC模式有很多优势,可实现单点控制,分离代码,快速开发,后期容易维护与扩展,代码架构清晰等等。
<?php // mvc pattern 单点控制 // 简单的控制器 C class NumberController extends DefaultController { public $model = null; public function __construct() { $this->model = NumberModel(); } // V public function view($value=0) { echo "The square of this number is: ",$this->model->square($value); } } // 控制器的基类 class DefaultController { public function run($action = 'index', $id = 0) { if (!method_exists($this, $action)) { $action = 'index'; } return $this->$action($id); } // 简单输出模版 public function index() { $html = ""; for($i = 1; $i < 10; $i++) { $html .= sprintf('<li><a href="index.php?a=view&m=number&id=%d"></a></li>', $i, "INDEX_$i"); } echo sprintf("<ul>%s</ul>", $html); } } // 一个非常简单的模型,计算四边形的面积 M class NumberModel { public function square($number) { return $number * $number; } } // 获取控制器,模型,和参数 $action = isset($_GET['a']) ? $_GET['a'] : 'index'; $module = isset($_GET['m']) ? $_GET['m'] : ''; $id = isset($_GET['id']) ? $_GET['id'] : ''; // 找到我们的控制器 switch($module) { case 'number': $controller = new NumberController(); break; default: $controller = new DefaultController(); break; } //<li><a href="index.php?a=view&m=number&id=1">Index_1</a></li> $controller->run($action, $id);

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

What does WeChat's Do Not Disturb mode do?

The easiest way to query the hard drive serial number

What is sleep mode used for on iPhone?

Do Not Disturb Mode Not Working in iPhone: Fix

iPhone 15 Pro: How to get rid of the silent mode symbol in the status bar

How to write a simple online reservation system through PHP

How to write a simple student performance report generator using Java?

PHP MVC Architecture: Building Web Applications for the Future
