使用 Yii2 执行注销

Executing logout with Yii2

我的布局上有一个按钮,点击它应该会退出。 布局:

<form action="/template/logout" method="POST">
   <input class="btn acc_exit" name="btn_logout" type="submit" value="logout" />
</form>

控制器:

public function actionLogout(){
    Yii::$app->user->logout();
    return Yii::$app->response->redirect('/login');
}

点击时出现此错误 msg

您需要在使用 POST 请求时添加 CSRF-TOKEN 将其添加到您的表单中。默认情况下启用 CSRF 保护,因此您需要提交一个令牌以及所有请求。通常它是通过隐藏字段完成的:

<form action="/template/logout" method="POST">
   <input id="form-token" type="hidden" name="<?=Yii::$app->request->csrfParam?>"
           value="<?=Yii::$app->request->csrfToken?>"/>
   <input class="btn acc_exit" name="btn_logout" type="submit" value="logout" />
</form>