The example in this article describes the usage of filter filter in Yii controller. Share it with everyone for your reference, the details are as follows:
Specify the filter action, (the following projectContext() method is used when calling new, list, and management pages)
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', ); }
Add some filter logic, Define the filtering method. 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; }
must be added before the filtering method. I hope this article will help you design PHP programs based on the Yii framework.
For more articles related to the analysis of filter usage in Yii controller, please pay attention to the PHP Chinese website!