Blogger Information
Blog 77
fans 0
comment 0
visits 55575
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
laravel框架MVC理解_1030
Jet的博客
Original
1205 people have browsed it

MVC:

M:model

V:view

C:controller

文件分类:

文件分类.jpg


index.php文件代码:

<?php

//phpinfo();
// 路由解析

$server = $_SERVER;
$path_info = $_SERVER['PATH_INFO'];  // /home/index

//$script_name = $_SERVER['script_name']; //脚本名称 /index.php

//$request_uri = $_SERVER['request_uri']; // /index.php/home/index
$path = ltrim($path_info,'/'); //去除字符串左边的字符
//分解$path为数组 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_method[0] . '.php';
$obj = new $controller();

$res = $obj->$method();

exit($res);


controller:home.php代码

<?php
/**

* 

*/

class Home

{

 

 public function index(){

  echo 'hello MVC';

 }
 //调用视图方法

 public function welcome(){

  //echo 'welcome China';

  require_once __DIR__ . '/../view/welcome.php';

 }
 //调用model方法

 public function Newsmodel(){

  require_once __DIR__ . '/../model/Newsmodel.php';

 } 

}


View:welcome.php代码

<!DOCTYPE html>

<html>

<head>

 <title>Welcome</title>

</head>
<body>

 <div style="font-size: 18px;text-align: center; color:red;">welcome China</div>

 <?php echo date('Y-m-d H:i:s')?>

</body>
</html>


model:newsmodel.php代码:

<!DOCTYPE html>

<html>

<head>

 <title>Newsmodel</title>

</head>
<body>

 <div style="font-size: 18px;text-align: center; color:red;">Newsmodel</div>

 <?php echo date('Y-m-d H:i:s')?>

</body>
</html>


运行结果截图:

welcome.jpg


总结理解截图:

图解.jpg




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