目录文件

controller/Index.php
fetch方法默认访问application/index/view/index/index.html
<?php
namespace app\index\controller;
use think\Controller;
class Index extends Controller
{
public function index()
{
return $this->fetch();
}
}
view/index/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
</head>
<body>
<h1>index下的index.html</h1>
</body>
</html>
运行结果如图

controller/article
fetch方法访问application/index/view/article/index.html
<?php
namespace app\index\controller;
use think\Controller;
class Article extends Controller
{
public function index()
{
return $this->fetch('article/index');
}
}
article/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>article</title>
</head>
<body>
<h1>article下的index.html</h1>
</body>
</html>
运行结果如图

配置文件引入样式
在index.php入口文件中

在index下添加配置文件config.php
<?php
return [
'view_replace_str' => [
'__PUBLIC__'=>SITE_URL.'/public/static/style/index',
]
];
此时__PUBLIC__的值设置为一个URL:http://localhost/php/Project/tp5/public/static/style/index
在对应路径下添加a.css文件
.mystyle{
font-size: 30px;
color: #953b39;
}
改变index/index.html中内容,引入css样式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
<link type="text/css" rel="stylesheet" href="__PUBLIC__/a.css">
</head>
<body>
__PUBLIC__<br>
<p class="mystyle">index下的index.html</p>
</body>
</html>
此时再次访问则字体大小与颜色改变

本文介绍了TP5框架中Controller层如何使用fetch方法与View层进行交互,展示了默认访问视图文件的规则,并详细讲解了如何在配置文件中引入样式,通过__PUBLIC__常量定位静态资源,以及实际应用中修改HTML内容并引入CSS样式来改变页面显示效果。

被折叠的 条评论
为什么被折叠?



