Laravel Error: The GET method is not supported for route login. Supported methods: POST.
This is weird, you think, because the codes in your routes/web.php didn’t even mention ‘GET’:
<?php
Route::post('/login', [UserController::class, 'login']);
But you’re still getting an error about ‘GET’. Why?
That is because a ‘GET’ route is missing!
When your login page is first loaded, you want to show a form. Since you’re not getting any input yet, there’s literally nothing in your $_POST.
Laravel sees this and thinks, “OK there’s no POST data, so it must be a GET.” So it goes looking for a GET route, but there’s none.
The solution is to add a GET route with the same path:
<?php
Route::get('/login', function () {
return view('login-form');
});
Route::post('/login', [UserController::class, 'login']);
Or, you could just use Route::view() to assign a view:
<?php
Route::view('/login', 'login-form');
Route::post('/login', [UserController::class, 'login']);
Or, if you want to handle it inside your controller class, you could use Route::any():
<?php
Route::any('/login', [UserController::class, 'login']);
When Route::any() is used and there’s no user input, you will receive an empty array when you check $request->input():
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function login (Request $request)
{
if (empty($request->input()) {
return view("participant-login");
}
// form validation and sanitisation goes here
}
}