方法一
直接上代碼:
<form action="?r=site/logout" method="post">
<input type="hidden" name="_csrf-frontend" value="<?php echo yii::$app->request->csrfToken?>"><input type="submit" value="退出">
</form>
只需要把方法一代碼添加上就能使用退出了,原理是通過yii框架的Yii::$app->user->logout()刪除登陸的cookie;
方法二
一句代碼退出:
<a href="<?= Url::toRoute('site/logout');?>">退出後臺</a>
但是你會發現它報錯了,它竟然報錯了
PHP Fatal Error – yii\base\ErrorException
Class 'Url' not found
這個錯誤是URL類不存在這時候在你要在執行退出的腳本是加一行代碼聲明,use yii\helpers\Url;
再去執行,你會發現又報錯了
Method Not Allowed (#405) Method Not Allowed. This url can only handle the following request methods: POST. The above error occurred while the Web server was processing your request. Please contact us if you think
this is a server error. Thank you.
這是因爲site控制器裏邊配置了
logout方式只能通過post方法接值,儘管這個地方沒有發送值,廢話不多說,直接看源代碼
如果允許GET訪問的話,那麼可以把箭頭指向的地方改成:get
再看修改完成後的代碼
OK 完美 。