Home >php教程 >PHP开发 >Analysis of filter usage in Yii controller

Analysis of filter usage in Yii controller

高洛峰
高洛峰Original
2016-12-30 14:44:261400browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn