tp5 controller与view通过fetch方法对应

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录文件

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>



此时再次访问则字体大小与颜色改变

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值