yii2 [行爲] behaviors 攔截器

yii2 攔截器數組

 

    在控制器中能夠自定義對action的攔截器,攔截器須要繼承 \yii\base\ActionFilteryii2

   參考代碼:yii

      

class BaseUserAuthorizeFilter extends ActionFilter
{
    public $rules = [];
    public $actions = [];

    /**
     * @param \yii\base\Action $action
     * @return bool
     */
    public function  beforeAction($action)
    {
        .....
        return true;
    }

    public function afterAction($action, $result)
    {
        
        return $result;
    }



}

控制器配置攔截器:spa

     其中 UserAuthorizeFilter 爲自定義攔截器 。code

  關鍵配置:blog

       only :僅對數組中action對應的請求地址有效繼承

       except:排除掉數組中action對應的請求地址有效get

 public function behaviors()
    {
        return [
            'access' => [
                'class' => UserAuthorizeFilter::className(),
               // 'only' => ['index'],
               // 'except' => ['getlist'],
                'rules' => [
                    [
                        'actions' => ['error'],
                        'allow' => true,
                    ],
                    [
                        'actions' => ['index'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
        ];
    }
相關文章
相關標籤/搜索