Blogger Information
Blog 19
fans 0
comment 0
visits 16744
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
用MVC搭建一个框架,自动加载视图 20191015
努力拼搏----赵桂福的博客
Original
855 people have browsed it

进入到小框架的学习,课堂上讲的还需要多多的在线下练习。到现在学习好一段时间了,总体来说还可以,但是融会贯通还是差了些。说到底收益还是满满的,感谢PHP中文网老师的辛苦付出。现将MVC搭建一个框架来自动加载视图实例呈现如下:

实例

<?php
//基础信息
header("Content-type: text/html; charset=utf-8");
date_default_timezone_set('PRC');
//判断参数,执行相关页面
$getPage = isset($_GET['a']) ? $_GET['a'] : 'index';
if($getPage != 'login'){
    echo "<script>location.href='mvc.php?a='+$getPage;</script>";	
}
//根据获得的参数进行判断执行加载相关页面
switch ($getPage){
 case 'login':
 	//2.设置模板名称
     $tplName = 'login_tpl'; break;

 case 'index':
 $tplName = 'index_tpl'; break;
}
//定义自动加载的视图的函数
function auto_load_tpl($tplName)
{
    // die($tplName);
    //开始根据参数$tplName 来判断进行加载相关的视图
    if (!empty($tplName)){

        switch ($tplName){
          case 'login_tpl':
   
           echo '
           <div style="width:50%;background:green;margin:0 auto;color:white; text-align:center; padding:10px;">
           <h2>登录</h2>
           <form>
           <label for="inputEmail">账号:</label>
           <input type="email" name="email" style="height:20px;">
           <label for="inputEmail">密码:</label>
           <input type="email" name="email" style="height:20px;">
           <button type="submit" >Sign in</button>
           </form>
           </div>
          ';
          break;
          case 'index_tpl':
          echo '
          <div style="width:50%;background:green;margin:0 auto;color:white; text-align:center; padding:10px;">
           <h2>Hi~~欢迎来到HOME页面,愉快的玩耍吧!</h2>
           </div>
          ';break;  
        }
    }
}
//执行自动加载
 auto_load_tpl($tplName); 
?>

运行实例 »

点击 "运行实例" 按钮查看在线实例

【总结】不知道这样算不算体现了MVC加载视图,目前是这样的理解,敬请老师指点迷津~~ 

Correction status:qualified

Teacher's comments:任何复杂的技术 , 都是由一些最简单的东西组成的, 从底层一点点做起很重要
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post