Reputation: 111
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
Upvotes: 11
Views: 25462
Reputation: 38652
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');
Upvotes: 14
Reputation: 39
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]);
Upvotes: 0
Reputation: 3007
Go to web.php and add get method for route:
use App\Http\Controllers\Auth\LoginController;
Route::get('logout', [LoginController::class,'logout']);
Upvotes: 0
Reputation: 2495
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.
Upvotes: 16
Reputation: 67
@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
Upvotes: 2
Reputation: 5731
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
Upvotes: 3
Reputation: 101
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
Upvotes: 2