Home > Back-end >  MethodNotAllowedHttpException at RouteCollection->methodNotAllowed(array('POST'))
MethodNotAllowedHttpException at RouteCollection->methodNotAllowed(array('POST'))

Time:11-02

I'm trying to use POST to let user log in with correct email/password. I keep getting this error

MethodNotAllowedHttpException at RouteCollection->methodNotAllowed(array('POST'))

web.php

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;

Route::get('/', function () {
    return view('login');
});

Route::post("/login",[UserController::class,'login']);

UserController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserController extends Controller
{
    //
    function login(request $req)
    {
        return $req->input();
    }
}

login.blade.php

@extends('master')
@section('content')
<div >
    <div >
        <div >
            <form action="/login" method="POST">
            <div >
                @csrf
                <labe for="exampleInputEmail">Email address</label>
                <input type="email" name="email"  id="exampleInputEmail" placeholder="Email">
            </div>
            <div >
                <labe for="exampleInputPassword">Password</label>
                <input type="password" name="password"  id="exampleInputPassword" placeholder="Password">
            </div>
            <button type="submit" >Login</button>
            </form>
        </div>
    </div>
</div>

@endsection

I tried opening the login page but POST is having issues. I expected the login page to use POST to check credentials in my database.

CodePudding user response:

Try this: Your routes might be cached. First clear your routes cache via terminal: php artisan route:clear

Afterwards, run php artisan route:list and verify that your routes are correct.

If that does not make the form work, then proceed further:

Give your login route a name as such:

Route::post("login",[UserController::class,'login'])->name('user.login');

Then pass it to your form action using the route() helper function like this:

@extends('master')
@section('content')
<div >
    <div >
        <div >
            <form action="{{ route('user.login') }}" method="post">
            <div >
                @csrf
                <label for="exampleInputEmail">Email address</label>
                <input type="email" name="email"  id="exampleInputEmail" placeholder="Email">
            </div>
            <div >
                <label for="exampleInputPassword">Password</label>
                <input type="password" name="password"  id="exampleInputPassword" placeholder="Password">
            </div>
            <button type="submit" >Login</button>
            </form>
        </div>
    </div>
</div>

@endsection
  • Related