暂时应该是没有必要的条件,但是我应该必须了解一下。
1. fetch() 方法不传入任何参数
这里我先直接在控制器中去使用 fetch() 方法。这里说明:
- 我的控制器去继承一下 think\Controller 基类,这样做的目的是去使用父类的方法 fetch()
- fetch() 方法不传入任何参数,会自动定位当前模块下的视图文件,如我这个方法 fun1() 定位的视图文件就是 test2\view\demo1\fun1.html。 ```php <?php
namespace app\test2\controller;
use think\Controller; // 这里继承 controller 基类是为了使用,其中的方法
class Demo1 extends Controller { public function fun1() { // fetch 方法是加载模板输出 return $this->fetch(); } }
对应的模板文件 test2\view\demo1\fun1.html```html<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Title</title></head><body><h3>我是fun1.html</h3></body></html>
postman 测试:
2. 先使用 assign() 再去使用 fetch()
assign 方法同样在 think\Controller 基类中存在,自如其名这个方法是赋值方法,赋值的是模板变量
我稍微改动一下刚才的控制器:
<?phpnamespace app\test2\controller;use think\Controller; // 这里继承 controller 基类是为了使用,其中的方法use app\common\model\User;class Demo1 extends Controller{public function fun1(){$user = User::get(1);// assign 去赋值模板变量$this->assign('var', $user);// fetch 方法是加载模板输出return $this->fetch();}}
再去改动一下对应的模板文件:
注意:特别注意一点,这里的语法是单花括号,不是胡须,不是胡须,不是胡须。我很容易在这里犯错误 ```html <!DOCTYPE html>
我是fun1.html
我要添加 模板变量了
{$var.name}
{$var.email}
{$var.gender}
postman 测试:<br />还有一点,我模型中设置了转换所以说 _获取器依然对模板文件是支持的哦_<br />---<a name="EQd5I"></a>## 3. 继承 think\Viewview() 方法和 fetch() 方法本来就是属于 视图层的方法,在基类View中也是存在的,所以完全可以View类```php<?phpnamespace app\test2\controller;use app\common\model\User;use think\View;class Demo1 extends View{public function fun1(){$user = User::get(2);// assign 去赋值模板变量$this->assign('var', $user);// fetch 方法是加载模板输出return $this->fetch();}}
postman 测试:
4. 静态代理
反正我要使用的方法在基类 Controller 和 View 类中都是存在的,为什么不使用静态代理搞一下捏
由于 Controller 类中的 fetch() 和 assign() 都是 protected 修饰的。
所以我选则静态代理 基类View,它其中的方法都是 public 修饰的
<?phpnamespace app\test2\controller;use app\common\model\User;//use app\test2\facade\View;use think\facade\View;class Demo1{public function fun1(){$user = User::get(2);// assign 去赋值模板变量View::assign('var', $user);// fetch 方法是加载模板输出return View::fetch();}}
其实我自定义一个静态代理的 View 也是可以OK的,就是我注释的代码。test2\facade\View.php 只是这么做完全没有必要的。
<?phpnamespace app\test2\facade;class View extends \think\Facade{// 获取要代理的类protected static function getFacadeClass(){return '\think\View';}}
5. 数组转换
可以使用toArray方法将当前的模型实例输出为数组,例如:
<?php$user = User::find(1);dump($user->toArray());
支持设置不输出的字段属性:
<?php$user = User::find(1);dump($user->hidden(['create_time','update_time'])->toArray());
数组输出的字段值会经过获取器的处理,也可以支持追加其它获取器定义(不在数据表字段列表中)的字段,例如:
<?php$user = User::find(1);dump($user->append(['status_text'])->toArray());
支持设置允许输出的属性,例如:
<?php$user = User::find(1);dump($user->visible(['id','name','email'])->toArray());
对于数据集结果一样可以直接使用(包括append、visible和hidden方法)
<?php$list = User::all();$list = $list->toArray();
V5.1.22+版本开始,可以在查询之前定义hidden/visible/append方法,例如:
<?phpdump(User::where('id',10)->hidden(['create_time','update_time'])->append(['status_text'])->find()->toArray());
注意,必须要首先调用一次Db类的方法后才能调用hidden/visible/append方法。
5.1 追加关联属性(先了解)
支持追加一对一关联模型的属性到当前模型,例如:
<?php$user = User::find(1);dump($user->append(['profile' => ['email', 'nickname']])->toArray());
profile是关联定义方法名,email和nickname是Profile模型的属性。
模型的visible、hidden和append方法支持关联属性操作,例如:
<?php$user = User::get(1,'profile');// 隐藏profile关联属性的email属性dump($user->hidden(['profile'=>['email']])->toArray());// 或者使用dump($user->hidden(['profile.email'])->toArray());
hidden、visible和append方法同样支持数据集对象。
6. JSON序列化
可以调用模型的toJson方法进行JSON序列化,toJson方法的使用和toArray一样。
<?php$user = User::get(1);echo $user->toJson();
可以设置需要隐藏的字段,例如:
<?php$user = User::get(1);echo $user->hidden(['create_time','update_time'])->toJson();
或者追加其它的字段(该字段必须有定义获取器):
<?php$user = User::get(1);echo $user->append(['status_text'])->toJson();
设置允许输出的属性:
<?php$user = User::get(1);echo $user->visible(['id','name','email'])->toJson();
模型对象可以直接被JSON序列化,例如:
<?phpecho json_encode(User::get(1));
输出结果类似于:
<?php{"id":"1","name":"","title":"","status":"1","update_time":"1430409600","score":"90.5"}
如果直接echo 一个模型对象会自动调用模型的toJson方法输出,例如:
<?phpecho User::get(1);
