Home php教程 php手册 简单的mvc模式实例

简单的mvc模式实例

Jun 06, 2016 pm 07:37 PM
mvc Example model Simple

Lynda 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);
Copy after login
Statement of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What does WeChat's Do Not Disturb mode do? What does WeChat's Do Not Disturb mode do? Feb 23, 2024 pm 10:48 PM

What does WeChat's Do Not Disturb mode do?

The easiest way to query the hard drive serial number The easiest way to query the hard drive serial number Feb 26, 2024 pm 02:24 PM

The easiest way to query the hard drive serial number

What is sleep mode used for on iPhone? What is sleep mode used for on iPhone? Nov 04, 2023 am 11:13 AM

What is sleep mode used for on iPhone?

Do Not Disturb Mode Not Working in iPhone: Fix Do Not Disturb Mode Not Working in iPhone: Fix Apr 24, 2024 pm 04:50 PM

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 iPhone 15 Pro: How to get rid of the silent mode symbol in the status bar Sep 24, 2023 pm 10:01 PM

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 online reservation system through PHP Sep 26, 2023 pm 09:55 PM

How to write a simple online reservation system through PHP

How to write a simple student performance report generator using Java? How to write a simple student performance report generator using Java? Nov 03, 2023 pm 02:57 PM

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

PHP MVC Architecture: Building Web Applications for the Future PHP MVC Architecture: Building Web Applications for the Future Mar 03, 2024 am 09:01 AM

PHP MVC Architecture: Building Web Applications for the Future

See all articles