Martin
Martin

Reputation: 557

Laravel 5.4.24 throws MethodNotAllowedHttpException during logout of users

I am working on an authentication system in Laravel 5.4.24. I get an error on my browser when trying to logout: MethodNotAllowedHttpException in RouteCollection.php on line 251.

The logout route in web.php in routes folder is:

Route::post('logout', 'Auth\LoginController@logout')->name('logout'); 

The controller stored app/Http/LoginController.php has the following code:

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;
    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }
}

Updated Question The code below is my login.blade.php

@extends('main')

@section('title', '| login')

@section('content')
    <div class='row'>
        <div class='col-md-6 col-md-offset-3'>
            {!! Form::open() !!}

                {{ Form::label('email', 'Email:') }}
                {{  Form::email('email', null, ['class' => 'form-control']) }}

                {{  Form::label('password', 'Password:') }}
                {{  Form::password('password', ['class' => 'form-control'])  }}

                <br>
                {{  Form::checkbox('remember') }} {{  Form::label('remember', 'Remember Me:') }}

                <br>
                {{  Form::submit('Login', ['class' => 'btn btn-primary btn-block']) }}


            {!!  Form::close()  !!}
        </div>
    </div>

@endsection

main.blade.php

<!DOCTYPE html>
<html lang="en">
<!-- Connection to the partials called _head.blade.php -->
@include('partials._head')
<body>

    @include('partials._nav')

    <!-- The below class container holds all body content-->
    <div class='container'>

        @include('partials._messages')

        {{ Auth::check() ? "Logged In" : "Logged Out"}}

        @yield('content')

        @include('partials._footer')
    </div> <!-- End of container -->

    @include('partials._javascript')
    @yield('scripts')
</body>
</html>

Upvotes: 6

Views: 12809

Answers (4)

Alejandro Gonzlez
Alejandro Gonzlez

Reputation: 24

Route::get('logout', function(){

     return back();

});

Route::post('logout', 'AuthController@logout');

Upvotes: 0

ramojo
ramojo

Reputation: 321

Just add this line to your routes/web.php

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

This fixes it perfectly without having to add any messy code.

Upvotes: 28

Junaid Qadir Shekhanzai
Junaid Qadir Shekhanzai

Reputation: 1425

Instead of using a form which I think clutters up the code, what you can do is create a new route named /logout and then bind it to the logout method in your Auth controller.

Jus add this line to your routes/web.php

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

Note: I appologise for my code markdown mess-up

Upvotes: 2

Hasan Tareque
Hasan Tareque

Reputation: 1741

Laravel 5.4+ uses post method for logout so instead of simple url (get) request you should post a form to logout.

In your main.blade.php file replace the following line:

{{ Auth::check() ? "Logged In" : "Logged Out"}}

with the following example, please change as it fit for your need - it is just example where to put these code.

 @if (Auth::guest())
                    <a href="{{ route('login') }}">Login</a>
                @else

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

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

                @endif

Upvotes: 14

Related Questions