简体   繁体   中英

Laravel 5.4 MethodNotAllowedHttpException error when enter wrong password after clearing cache

I have login form with two text fields email, password.when I tried to login with credentails it's working fine but when clear cache and then tried to login it gives the 'MethodNotAllowedHttp' exception.I am not getting the issue why it's showing this error. My code is as follows:

   Route::post('users/login/5', 'administrator\usersController@login')->name('sl'); 


    usersController.php
    namespace App\Http\Controllers\administrator;
    use Illuminate\Http\Request;
    use App\Http\Controllers\Controller;
    use Illuminate\Support\Facades\Auth;
    use Illuminate\Validation\Validator;
    use Session;

    class FreeusersController extends Controller {

        function login(Request $request) {
            $type = $request->segment(3);

            //print_r($request);
            echo "type=".$type; 
            echo $request->input('email');
            echo $request->input('password');

            die("sss");

            if ($request->isMethod('post') && !empty($type)) {
                $this->validate($request, [
                    'email' => 'required|min:5,max:50',
                    'password' => 'required|min:5,max:50'
                ]);
                switch ($type) {
                    case 5:
                        $condArr = ['email' => $request->input('email'), 'password' => $request->input('password'), 'type' => '5', 'role' => 'father'];
                        break;
                    case 4:
                        $condArr = ['email' => $request->input('email'), 'password' => $request->input('password'), 'type' => '4', 'status' => true];
                        break;
                }

                if (Auth::attempt($condArr)) {
                    return redirect('administrator/dashboard');
                } else {
                    return redirect(url()->previous())->withErrors(['password' => 'Invalid credentials'])->withInput();
                }

            } else {
                return redirect("/");
            }
        }
    }

<form action="/users/login/5" id="login-form" method="post" class="smart-form client-form">

    {{ csrf_field() }}
    <fieldset>
        <section>
            <label class="label">Email</label>
            <label class="input"> <i class="icon-append fa fa-user"></i>
                <input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}">
        </section>
        <section>
            <label class="label">Password</label>
            <label class="input"> <i class="icon-append fa fa-lock"></i>
                <input id="password" type="password" class="form-control" name="password">
        </section>
    </fieldset>
    <footer>
        <button type="submit" class="btn btn-primary">
            LogIn
        </button>
    </footer>
</form>

You have to clear routes:

php artisan route:cache

Then it will remember that login action is post.

I think you are getting this error because you when you are calling this url there is another url with something like users/{id} which means anything after users/ . May be you are using resource route. So, when you call url users/login/5 its taking login/5 as $id of that users/{id} url.

But that url is for GET method. You are calling this with post method, as a result you are getting this error.

Solution

You can try calling your url using route method like:

action="{{route('sl', ['id'=>5])}}"

If it doesn't work then you can change your route to something else. For example:

Route::post('user/login/5', 'administrator\\usersController@login')->name('sl');

Here you can use user instead of users because you already have a url with a users . Don't forget to change your action too.

First look at your routes with php artisan route:list.

Then are you sure of your request verb ? Look in your navigator console to look at your requests.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM