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

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);
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.