Blogger Information
Blog 27
fans 0
comment 0
visits 26618
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
MVC运行原理-2019年10月31日
渊的博客
Original
947 people have browsed it

1、入口文件index.php


实例<?php
// 路由解析
$server = $_SERVER;
$script_name=$_SERVER['SCRIPT_NAME'];
$request_uri=$_SERVER['REQUEST_URI'];
// exit($script_name);
$path_info=str_replace($script_name, '', $request_uri);
// exit($request_uri);
// $path_info=$_SERVER['PATH_INFO'];// /home/index
$path=ltrim($path_info,'/');

//解析controller和method
$controller_method=explode('/',$path);
$controller_method[0]=ucfirst($controller_method[0]);

$controller=$controller_method[0];
$method=$controller_method[1];
require_once __DIR__.'/controller/'.$controller.'.php';
$obj=new $controller();
$res=$obj->$method();
exit($res);

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


2、Controller 文件Home.php


实例<?php

class Home{
    public function index(){
        require_once __DIR__.'/../view/index.php';
    }

    public function  welcome(){
        require_once __DIR__.'/../view/welcome.php';
    }
}
运行实例 »点击 "运行实例" 按钮查看在线实例


3、 View 模板文件 index.php


实例<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>商味书屋——首页</title>
</head>
<body>
    <div style="font-size:32px;text-align: center;color:red;">这里是商味书屋首页</div>
    <?php echo date('Y-m-d H:i:s'); ?>
</body>
</html>
运行实例 »点击 "运行实例" 按钮查看在线实例


运行效果图


mvc.png

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!