Home  >  Article  >  PHP Framework  >  How to use yii filter

How to use yii filter

angryTom
angryTomOriginal
2020-03-11 16:54:372480browse

How to use yii filter

How to use yii filter

A filter is a piece of code that can be configured to be executed before or after the controller action is executed.

For example, access control filters will be executed to ensure that the user is authenticated before performing the requested action;

Performance filters can be used to measure the time it takes for the controller to execute.

An action can have multiple filters. Filters are executed in the order they appear in the filter list. Filters can prevent actions and other subsequent filters from executing.

The public function filters() method of the controller must be overridden in the controller to set which filter affects which action.

Usage example:

class CController{
    public function filterAccessControl($filterChain)
    {
       //这里是你的逻辑
        
       $filterChain->run();
    }
}
 
class UserController extents CController{
    public function filters()  
    {  
      return array{
         'AccessControl' // 这里就过滤了
      }
    }
  public function actionIndex(){
        
  }
}

(Related tutorial recommendations: yii framework)

The above is the detailed content of How to use yii filter. For more information, please follow other related articles on 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