岗涂计供勾录僵物痛显录盾芭
D:\phpStudy\PHPTutorial\WWW\tp5\application\index\controller\Index.php:
<?php namespace app\index\controller; class Index extends \think\Controller { public function index() { //直接渲染模板输出 return $this->fetch(); } }
D:\phpStudy\PHPTutorial\WWW\tp5\application\index\view\index\index.html:
<p>我是index控制器index操作的模板文件</p>
执行:
创建User控制器,并创建对应的模板文件
D:\phpStudy\PHPTutorial\WWW\tp5\application\index\controller\User.php:
<?php namespace app\index\controller; class User extends \think\Controller { public function login() { //直接渲染模板输出 return $this->fetch(); } }
D:\phpStudy\PHPTutorial\WWW\tp5\application\index\view\user\login.html:
<p>我是User控制器下的login操作的模板文件</p>
执行:
1.在index控制器中的index操作中访问user控制器下的login操作对应的模板
D:\phpStudy\PHPTutorial\WWW\tp5\application\index\controller\Index.php:
<?php namespace app\index\controller; class Index extends \think\Controller { public function index() { //直接渲染模板输出 return $this->fetch('user/login'); } }
执行:
2.跨模块访问
创建admin模块,并创建模板文件
demo.html:
<p>我是admin模块下index控制器的demo操作对应的模板文件</p>
D:\phpStudy\PHPTutorial\WWW\tp5\application\index\controller\Index.php:
<?php namespace app\index\controller; class Index extends \think\Controller { public function index() { //直接渲染模板输出 //访问admin模块下的index控制器下的demo操作 return $this->fetch('admin@index/demo'); } }
执行:
删除user控制器,此时user目录既没有对应的控制器也没有对应的方法
D:\phpStudy\PHPTutorial\WWW\tp5\application\index\controller\Index.php:
<?php namespace app\index\controller; class Index extends \think\Controller { public function index() { //直接渲染模板输出 return $this->fetch('user/login'); } }
执行,此时仍然能够访问user/login.html模板文件
这说明模板文件是独立于控制器和操作而存在的
3.直接访问视图(view)根目录下的模板文件
在view目录下直接创建模板文件test.html
test.html:
<p>我是视图目录view下的模板文件test</p>
D:\phpStudy\PHPTutorial\WWW\tp5\application\index\controller\Index.php:
<?php namespace app\index\controller; class Index extends \think\Controller { public function index() { //直接渲染模板输出 return $this->fetch('/test'); } }
执行:
4.访问应用的入口文件public目录下的模板文件
在public 中创建tpl/demo.html模板文件
demo.html:
<p>我是应用入口目录tpl中的模板文件demo</p>
D:\phpStudy\PHPTutorial\WWW\tp5\application\index\controller\Index.php:
<?php namespace app\index\controller; class Index extends \think\Controller { public function index() { //直接渲染模板输出 //访问public目录一定要加.和后缀.html return $this->fetch('./tpl/demo.html'); } }
执行:
暗鳞相迷庇括反了哎缅筹船爆