yii小记
php备忘

YII知识小结:
1.YII默认控制器为 SiteController 默认控制器可以通过配置文件 application/config/main.php修改,指定 defaultController即可, 如 'defaultController' => 'home',
2.控制器的默认方法为actionIndex 可以通过定义控制器属性 defaultAction来设置,如 public $defaultAction = 'lists';
3.获取Controller ID Yii::app()->getController()->id 获取Action ID Yii::app()->getController()->getAction()->id 类:system.web.CWebApplication
4.获取POST参数 Yii::app()->request->getPost('name'); 获取GET参数 Yii::app()->request->getQuery('name');
5.判断提交方式 Yii::app()->request->isPostRequest Yii::app()->request->isAjaxRequest 类:system.web.CHttpRequest
6.view中导入js、css、Meta 类:system.web.CClientScript
//导入CSSYii::app ()->clientScript->registerCssFile ( Yii::app()->baseUrl.'/uploadify.css', CClientScript::POS_HEAD );//导入YII集成JQUERYYii::app ()->clientScript->registerCoreScript ( 'jquery' );//导入外部JSYii::app ()->clientScript->registerScriptFile ( Yii::app()->baseUrl.'/jquery.uploadify.min.js', CClientScript::POS_HEAD );//Meta信息Yii::app()->clientScript->registerMetaTag('关键字','keywords'); Yii::app()->clientScript->registerMetaTag('些描述','description'); Yii::app()->clientScript->registerMetaTag('作者','author'); Yii::app()->clientScript->registerMetaTag(' text/html;charset=utf-8', null, 'Content-Type');
7.生成URL可以使用 Yii::app()->createUrl('post/read', array('id' => 1))
8.main.php引用其他配置文件可用'params'=> require(dirname(__FILE__).'/params.php'), params.php直接返回数组即可,如return array();访问方式则为Yii::app()->params['paramName']
9.数据在保存、查找、验证前后调用before、afeter方法,如:beforeSave、afterSave、beforeValidate
protected function beforeSave() { if(parent::beforeSave()) { if($this->isNewRecord) { $this->create_time = date("Y-m-d H:i:s"); } else { $this->update_time = date("Y-m-d H:i:s"); } return true; } else { return false; } }
10.表单验证时可以指定应用场景,如$model = new Post('create') 或者通过$post->scenario = 'create' 指定
11.获取http://127.0.0.1/test/index?var=val的内容:
Yii::app()->request->getUrl /test/index?var=val Yii::app()->request->getHostInfo http://127.0.0.1Yii::app()->request->getPathInfo test/index Yii::app()->request->getRequestUri /test/index?var=val Yii::app()->request->getQueryStringvar=val
12.判断是否为AJAX动作:
Yii::app()->request->isAjaxRequest
13.GET,POST,REQUEST
Yii::app()->request->getParam('id');//requestYii::app()->request->getQuery('id');//getYii::app()->request->getPost('id');//post