Yii框架基础函数用法概述
本示例详细解析了Yii框架中几个关键函数的基础使用方法,旨在为读者提供实用的参考代码与说明。
1. redirect
return $this->redirect(['login']);
redirect方法是对底层响应重定向逻辑的封装,其效果等同于直接操作response对象。
$response=Yii::app->response();
$response->headers->add('location','www.baidu.com');
2. save
save方法的第一个参数控制是否执行验证流程,第二个参数指定需要保存的属性字段列表;若该参数为空,则默认保存所有加载的属性。
当调用save方法时,若传入两个参数,必须明确指定对应的属性名称。
* @param bool $runValidation whether to perform validation (calling [[validate()]])
* before saving the record. Defaults to `true`. If the validation fails, the record
* will not be saved to the database and this method will return `false`.
* @param array $attributeNames list of attribute names that need to be saved. Defaults to null,
* meaning all attributes that are loaded from DB will be saved.
* @return bool whether the saving succeeded (i.e. no validation errors occurred).
*/
public function save($runValidation = true, $attributeNames = null)
3. 全局命名空间引用
在类名或函数名前添加反斜杠(\),表示引用全局命名空间下的资源。
4. as 别名机制
在一个类文件中,可以多次使用as关键字为同一类起不同的别名,以便在不同上下文中灵活引用。

5. 设置布局文件
![]()
order::classname---表示的order的AR类的所有的属性
---表示的order的AR类的所有的属性