10

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
2
  • Please, add your controller code and other codes (e.g. web.php, middlware). Unless it is hard to solve your issue. Thanks. Commented Oct 10, 2019 at 6:33
  • Are you using the default laravel auth routes by calling "Auth::routes()" in your routes file?
    – thmspl
    Commented Oct 10, 2019 at 7:00

7 Answers 7

15

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.

14

It is recommended to use a form instead of href. CSRF protection. Update in 2024-07

@if(Auth::check())
  <li>
    <i class="fa fa-user"></i> {{ Auth::user()->name }}:
    <form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: inline;">
      @csrf
      <button type="submit" style="background: none; border: none; color: #007bff; text-decoration: underline; cursor: pointer; padding: 0;">
        Logout
      </button>
    </form>
  </li>
@else
  <li>
    <a href="{{ route('login') }}"><i class="fa fa-user"></i>Login</a>
  </li>
@endif

(OLD Answer. For reference only)

Use

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

and in route file

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

    return Redirect::to('/');
})->name('logout');

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
0
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

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']);
0

If You need GET method You need to use own routes as it is explained above. But for default routes use javascript and onclick event on a tag and submit hidden form with POST method. It let You preserve menu layout.

<a class="dropdown-item" href="{{ route('logout') }}" onclick="event.preventDefault(); document.getElementById('logout-form').submit();"> {{ __('Logout') }} </a><form id="logout-form" action="{{ route('logout') }}" method="POST" class="d-none">@csrf</form>

In web file routes may be used in default way as:

Auth::routes(['verify' => true]);

Not the answer you're looking for? Browse other questions tagged or ask your own question.