为什么在 Laravel 中注销路由不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39342903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:24:57  来源:igfitidea点击:

Why does not work logout route in Laravel?

laravellaravel-5.2laravel-5.3

提问by Babaev

When I try to logout from admin panel I get error:

当我尝试从管理面板注销时,出现错误:

MethodNotAllowedHttpException in RouteCollection.php line 218:

But in routing there is route logout:

但是在路由中有路由logout

POST | logout |  App\Http\Controllers\Auth\LoginController@logout | web  

How can I fix this?

我怎样才能解决这个问题?

回答by MECU

If you have upgraded to Laravel 5.3, the get logout route was purposefully removed during the upgrade.

如果您已升级到 Laravel 5.3,则在升级过程中有意删除了 get logout 路由。

POST To Logout

The Auth::routes method now registers a POST route for /logout instead of a GET route. This prevents other web applications from logging your users out of your application. To upgrade, you should either convert your logout requests to use the POST verb or register your own GET route for the /logout URI

发布到注销

Auth::routes 方法现在为 /logout 注册一个 POST 路由,而不是一个 GET 路由。这可以防止其他 Web 应用程序将您的用户从您的应用程序中注销。要升级,您应该将注销请求转换为使用 POST 动词或为 /logout URI 注册您自己的 GET 路由

https://laravel.com/docs/5.3/upgrade

https://laravel.com/docs/5.3/upgrade

You need to change the have all logout links to forms so it can POST to the route. From https://github.com/acacha/adminlte-laravel/issues/94, something like:

您需要将所有注销链接更改为表单,以便它可以发布到路由。来自https://github.com/acacha/adminlte-laravel/issues/94,类似于:

<a href="{{ url('/logout') }}" onclick="event.preventDefault(); document.getElementById('logout-form').submit();"> Logout </a>

<form id="logout-form" action="{{ url('/logout') }}" method="POST" style="display: none;">{{ csrf_field() }} </form>

Source

来源

回答by maudev

You need to do the following steps:

您需要执行以下步骤:

1.- if you're working with a class created by you to the login, specify it in config/auth.php:

1.- 如果您正在使用由您创建的登录类,请在 config/auth.php 中指定它:

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\Administrator::class,
    ],
],

2.- your model must inherit from class Authenticatable:

2.- 您的模型必须继承自Authenticatable类:

use Illuminate\Foundation\Auth\User as Authenticatable;
class Administrator extends Authenticatable{
    #code...
}

3.- add the logout() function on your LoginController, import Authand Redirectclasses

3.- 在 LoginController 上添加 logout() 函数,导入AuthRedirect

public function logout(){
    Auth::logout();
    return Redirect::to('admin');
}

3.- specify the route that you will use to logout via GET

3.- 指定您将用于通过 GET 注销的路由

Route::get('logout','LoginController@logout');

It is all, this should work.

就是这样,这应该有效。

NOTE:Check if you are authenticated by the function Auth::check()

注意:检查您是否通过功能验证Auth::check()

Additional information:When you are working with POST requests, Laravel needs to verify that the request are not a malicious request, for this Laravel needs an ecrypted code, this is called csrf_token, if you don't send this, by default all your requests will be not allowed.

附加信息:当你处理 POST 请求时,Laravel 需要验证请求不是恶意请求,因为这个 Laravel 需要一个加密代码,这称为csrf_token,如果你不发送它,默认情况下你所有的请求将不被允许。

回答by Zaki Ahmed

is your LoginController in Auth folder or Controllers folder.. Maybe You're giving a wrong path.. if the LoginController is in Controllers folder, the Path should be Route::get('logout', 'App\Http\Controllers\LoginController@logout');

是您在 Auth 文件夹或 Controllers 文件夹中的 LoginController .. 也许您提供了错误的路径.. 如果 LoginController 在 Controllers 文件夹中,则路径应该是 Route::get('logout', 'App\Http\Controllers\LoginController @登出');

回答by halloei

You're trying to access a POSTroute with GET.

您正在尝试POST使用GET.

You could:
- create an ajax request with POST
- wrap the button in a POST-form
- create an additional GET-route to App\Http\Controllers\Auth\LoginController@logout

您可以:
-创建一个Ajax请求POST
-在一个包裹按钮POST-体
-创建一个额外的GET-route到App\Http\Controllers\Auth\LoginController@logout

回答by noodles_ftw

You're doing a GETrequest to a POSTroute. Create or modify the existing route to accept GETrequests (a redirect via a link is a GETrequest (most of the time))

您正在GETPOST路线发出请求。创建或修改现有路由以接受GET请求(通过链接重定向是GET请求(大多数情况下))

回答by Deepak

Replace

代替

<a href="{{ url('/logout') }}" class="btn btn-default btn-flat">{{ trans('adminlte_lang::message.signout') }}</a>

with

<a href="{{ url('/logout') }}" class="btn btn-default btn-flat" onclick="event.preventDefault(); document.getElementById('logout-form').submit();"> {{ trans('adminlte_lang::message.signout') }} </a>

<form id="logout-form" action="{{ url('/logout') }}" method="POST" style="display: none;">{{ csrf_field() }} </form>

in mainheader.blade.php and it will fix.

在 mainheader.blade.php 中,它将修复。

回答by HMagdy

Simply you can add this to routes file:

只需将其添加到路由文件中即可:

Route::get('/logout' , 'Auth\LoginController@logout');