MethodNotAllowedHttpException - No message

Published 3 weeks ago by siusiak1000

Hello! I have a problem. Log in, registration is working but when I want to log out I get this error: MethodNotAllowedHttpException - No message

Error link: http://gazeta1.nazwa.pl/mesiek/error.html

tykus
tykus
3 weeks ago (391,190 XP)

You are making a GET request - Laravel's Auth scaffolded logout changed to a POST request some time ago.

siusiak1000

Thank you for your answer!

I have used "php artisan make: auth" and received in web.php - "Auth :: routes ();". Where can I change GET to POST?

tykus
tykus
3 weeks ago (391,190 XP)

Somewhere you have a link to logout, right? Remove the link and replace it with a simple form:

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

Or otherwise, if you;re using AJAX, send a POST request rather than a GET

siusiak1000

"The page has expired due to inactivity. Please refresh and try again."

It should work even mydomain.com/logout and it does not work.

tykus
tykus
3 weeks ago (391,190 XP)

Edited above to add the csrf_field()

siusiak1000

It works! But I would like it to work like this:

<li><a href="{{ route('logout') }}">Log out!</a></li>​
tykus
tykus
3 weeks ago (391,190 XP)

Then you would need to make your own route to respond to a GET request:

Route::get('logout', 'Auth\[email protected]');
siusiak1000

@logout doesnt exist.

Some time ago I created another project and everything worked. All auth scripts are the same in both projects. This does not work and the previous one works.


<?php

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 = '/videos';

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


Snapey
Snapey
3 weeks ago (708,325 XP)

if you want to check the names of routes, use php artisan route:list

Its a security enhancement that the logout route is POST and not GET, so, it may have just worked before but things move on...

siusiak1000
Route::get('logout', 'Auth\[email protected]');

It works! Why laravel did not automatically assign a method POST?

tykus
tykus
3 weeks ago (391,190 XP)

Sorry, updated previous reply... the fully qualified class name for the LoginController was unnecessary.

Why laravel did not automatically assign a method POST?

What do you mean? Auth::routes(); registers all authentication routes including logout!

siusiak1000

Auth::routes(); register all routes correctly except logout and POSTs it instead of GET. Why is it like that?

tykus
tykus
3 weeks ago (391,190 XP)

Because that's what the framework does... https://laravel.com/docs/5.3/upgrade

POST To Logout The Auth::routes method now registers a POST route for /logout instead of a GET route. This prevents other web applications from logging your users out of your application. To upgrade, you should either convert your logout requests to use the POST verb or register your own GET route for the /logout URI: Route::get('/logout', 'Auth\[email protected]');

Please sign in or create an account to participate in this conversation.