本文實例講述了Yii控制器中filter過濾器用法。分享給大家供大家參考,具體如下:
指定過濾動作,(如下projectContext()方法在新建,列表,管理頁面調用時使用)
public function filters() { return array( 'accessControl', // perform access control for CRUD operations 'postOnly + delete', // we only allow deletion via POST request 'projectContext +create index admin', ); }
添加一些過濾器邏輯,定義過濾方法,過濾方法前必加filter
public function filterProjectContext($filterChain) { $prijectId = null; if(isset($_POST['pid'])){ $projectId = $_POST['pid']; }elseif(isset($_GET['pid'])){ $projectId = $_GET['pid']; } $this->loadProject($projectId); $filterChain->run(); } public function loadProject($projectid) { if($this->_project === null){ $this->_project = Project::model()->findbyPK($projectid); if($this->_project === null){ throw new CHttpException(404,'请求和项目没找到!'); } } return $this->_project; }
希望本文所述對大家基於Yii框架的PHP程式設計有所幫助。
更多Yii控制器中filter過濾器用法分析相關文章請關注PHP中文網!