Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 460
Martin Avatar asked Jun 23 '17 08:06

Martin


2 Answers

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.

like image 170
ramojo Avatar answered Oct 26 '22 23:10

ramojo


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
like image 36
Hasan Tareque Avatar answered Oct 26 '22 22:10

Hasan Tareque