9

I am getting error when i am logout, it's showing me this error..."The GET method is not supported for this route. Supported methods: POST." Please help me to solve this issue..

Here are my code...

@if(Auth::check())
  <li><i class="fa fa-user"></i> {{Auth::user()->name}}:
    <a href="{{url('logout')}}">logout</a>
  </li>
@else
  <li>
    <a href="{{route('login')}}"><i class="fa fa-user"></i>Login</a>
  </li>
@endif
thmspl
  • 2,437
  • 3
  • 22
  • 48
sumit group3
  • 91
  • 1
  • 1
  • 7
  • Please, add your controller code and other codes (e.g. web.php, middlware). Unless it is hard to solve your issue. Thanks. – Muminur Rahman Oct 10 '19 at 06:33
  • Are you using the default laravel auth routes by calling "Auth::routes()" in your routes file? – thmspl Oct 10 '19 at 07:00

6 Answers6

14

You could just add this line in your web.php routes file:

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

This allows you to logout by using a GET Request.

thmspl
  • 2,437
  • 3
  • 22
  • 48
9

Use

<a href="{{ route('logout') }}">Logout</a>

and in route file

Route::get('logout', function ()
{
    auth()->logout();
    Session()->flush();

    return Redirect::to('/');
})->name('logout');
Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
3

GET method in not supported for logout. Laravel 5.4+ uses post method for logout so instead of simple GET request you should POST a form to logout.

Ex. :

<form id="logout-form" action="{{ url('logout') }}" method="POST">
            {{ csrf_field() }}
    <button type="submit">Logout</button>
</form>

Change in your code :

@if(Auth::check())
<li><i class="fa fa-user"></i> {{Auth::user()->name}}:</li>
        <form id="logout-form" action="{{ url('logout') }}" method="POST">
                    {{ csrf_field() }}
            <button type="submit">Logout</button>
        </form>
      @else
    <li><a href="{{route('login')}}"><i class="fa fa-user"></i>
            Login
        </a>
    </li>
@endif
Yasin Patel
  • 5,624
  • 8
  • 31
  • 53
2

You're using the href attribute of a link to call the according URL - these links however always use GET HTTP calls to open/call the according target. As the error message states, the target you're calling is expecting a POST HTTP call.

Knowing what your problem is, you'd probably find this StackOverflow answer which should help you resolve the problem in a way that suits you: Making href anchor tag request post instead of get

illoOminated
  • 101
  • 3
2
@if(Auth::check())
  <li><i class="fa fa-user"></i> {{Auth::user()->name}}:
    <a href="{{ route('logout') }}" onclick="event.preventDefault();document.getElementById('frm-logout').submit();">Logout</a>    
    <form id="frm-logout" action="{{ route('logout') }}" method="POST" style="display: none;">
        {{ csrf_field() }}
    </form>
  </li>
@else
  <li>
    <a href="{{route('login')}}"><i class="fa fa-user"></i>Login</a>
  </li>
@endif
0

From Laravel 8 to logout with get method

Go to web.php and add get method for route:

use App\Http\Controllers\Auth\LoginController;
Route::get('logout', [LoginController::class,'logout']);
Eng_Farghly
  • 1,987
  • 1
  • 23
  • 34