用AI赚第一桶💰低成本搭建一套AI赚钱工具,源码可二开。 广告
[TOC] ### 基本视图的使用 控制器`Index`: ~~~php <?php namespace module\index\controller; use lying\base\Controller; class IndexCtrl extends Controller { public function index() { return $this->render(); } } ~~~ 在控制器对应视图目录`module/index/view/index/`下定义视图文件`index.html`: ~~~ <h1>欢迎使用Lying</h1> ~~~ 当你访问这`index`方法的时候,你就能看见大大的'欢迎使用Lying'。 如果带上参数: ~~~php <?php namespace module\index\controller; use lying\base\Controller; class IndexCtrl extends Controller { public function index() { return $this->render('welcome'); } } ~~~ 那么对应的视图文件就是`module/index/view/index/welcome.html`咯 ### 视图赋值 控制器`Index`: ~~~php <?php namespace module\index\controller; use lying\base\Controller; class IndexCtrl extends Controller { public function index() { $this->assign('name', 'Lying'); $this->assign(['id'=>10, 'sex'=>'男']); return $this->render(); } } ~~~ 在控制器对应视图目录`module/index/view/index/`下定义视图文件`index.html`: ~~~php <h1>欢迎使用{$name} {$id} {$sex}</h1> ~~~ 当你访问这`index`方法的时候,你还是能看见大大的`'欢迎使用Lying 10 男'`。 ### 视图文件路径 > 如果你的控制器名不想和视图文件夹名称对应,或者想使用其他视图目录下的视图文件或者布局文件,你可以这样做: ~~~php return $this->render('welcome'); //使用当前module下'view/控制器ID/welcome.html' return $this->render('my/welcome'); //使用当前module下'view/控制器ID/my/welcome.html' return $this->render('/welcome'); //使用当前module下'view/welcome.html' return $this->render('/welcome/t'); //使用当前module下'view/welcome/t.html' ~~~ ### 使用自定义视图路径 视图的路径默认是 `module/当前模块/view`,你可以自定义模板路径: ~~~php <?php namespace module\index\controller; use lying\base\Controller; class IndexCtrl extends Controller { public function init() { parent::init(); $this->viewPath = DIR_ROOT . DS . 'tpl'; //自定义视图路径 } public function index() { //这时候模板路径就是 DIR_ROOT . DS . 'tpl/index/index.html' return $this->render(); } } ~~~