Yii2: Method Not Allowed (#405) while logout user

前端 未结 7 1073
无人及你
无人及你 2020-12-16 03:02

I am loging out user through following code. This is my view code behind logout button:

  • \"&g
  • 7条回答
    •  难免孤独
      2020-12-16 03:19

      Seems like you have VerbFilter attached to logout action in your SiteController:

      /**
       * @inheritdoc
       */
      public function behaviors()
      {
          return [            
              'verbs' => [
                  'class' => VerbFilter::className(),
                  'actions' => [
                      'logout' => ['post'],
                  ],
              ],
          ];
      }
      

      That means this action can requested only with POST method, and you are requesting with GET, that's why exception #405 is thrown.

      Either remove this from VerbFilter or add data-method attribute to request with POST:

      ...
      

      Update: Another reason of this problem can be missing dependency for yii\web\YiiAsset. Make sure it's included in AppAsset:

      public $depends = [
          'yii\web\YiiAsset',
          ...
      ];
      

      YiiAsset provides data-method attribute feature which gives possibility to link act as a form with action post by writing less code. Without asset obviously link will act as regular link and standard GET request will be sent.

    提交回复
    热议问题