1. 程式人生 > >tp5 controller與view通過fetch方法對應

tp5 controller與view通過fetch方法對應

目錄檔案

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>



此時再次訪問則字型大小與顏色改變