MVC

MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写

一个简单的例子就是

 当一个网站打开后,由用户(客户端)点击一篇文章(也就是视图View)后,将发送HTTp协议请求到对应的控制器(control)中对应的一个方法里,然后由控制器调用对应的model文件中对应的方法(通常model文件我们一般用来处理逻辑上的问题,比如说连接数据库之类的操作),然后有model返回一个执行完毕结果集给控制器,然后控制器来分配到对应的视图View文件。

下面是我们后台管理系统文件组织的例子:

详细的例子:

 

      代码示例开始:

               1、入口文件 index.php

    <?php
        $module = $_REQUEST['c'];
        $control = (!empty($control)?$control:'home');
        $action = $_REQUEST['a'];
        $action = (!empty($action)?$action:'index');
        $file = $control.'.php';
        if (is_file($file))
        { 
            require $control.'.php';
            $class = ucfirst(strtolower($control));
            $method =  strtolower($action)."Action";
            $obj = new $class();
            $output = $obj->$method();
        }
    ?>

     2、控制器  home.php

    <?php
        include 'model.php';
        class Home
        {
            function __construct()
            {
            
            }
            function indexAction()
            {
                $model = new Model();
                $exam = $model->GetIndex();
                var_dump($exam);
            }
            function test1Action()
            {
                $model = new Model();
                $exam = $model->GetTest1();
                var_dump($exam);
            }
        }
    ?>

     3、Modle文件 model.php
<?php
    class Model
    {
        public function GetIndex()
        {
            return 'index';
        }
        public function GetTest1()
        {
            return 'test1';
        }
    }
?>

   4、视图文件 view.html

   <a href="index.php?c=home&a=index">home->index</a>
   <a href="index.php?c=home&a=test1">home->test1</a>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值