Yii2:注销用户时不允许使用方法(#405) [英] Yii2: Method Not Allowed (#405) while logout user

查看:27
本文介绍了Yii2:注销用户时不允许使用方法(#405)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过以下代码注销用户.这是我在注销按钮后面的查看代码:

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

<li>
   <a href="<?= Url::to(['site/logout'])?>">
    <i class="fa fa-sign-out"></i> Log out
   </a>
 </li>

我的控制器代码是:

public function actionLogout()
{
    Yii::$app->user->logout();

    $model = new LoginForm();
    $this->layout = 'index';
    return $this->render('login', ['model' => $model]);
}

在注销时它显示我:

方法不允许.这个url只能处理以下请求方法:POST.

Method Not Allowed. This url can only handle the following request methods: POST.

这是什么?

推荐答案

似乎您在 SiteController 中将 VerbFilter 附加到 logout 操作:

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

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

这意味着此操作只能使用 POST 方法请求,而您使用 GET 请求,这就是抛出异常 #405 的原因.

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

要么从 VerbFilter 中删除它,要么添加 data-method 属性以使用 POST 请求:

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

<a href="<?= Url::to(['site/logout'])?>" data-method="post">...</a>

更新:这个问题的另一个原因可能是缺少对的依赖yii\web\YiiAsset.确保它包含在 AppAsset 中:

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 提供了 data-method 属性特性,可以通过编写更少的代码来将行为作为带有动作 post 的表单进行链接.没有资产显然链接将作为常规链接并发送标准 GET 请求.

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.

这篇关于Yii2:注销用户时不允许使用方法(#405)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆