简体   繁体   中英

Laravel 5.6 MethodNotAllowedHttpException when user is logged out but sending a post request

I am using Sentinel for authentication. As long as I am logged in, everything is working well. But when I am logged out (fe delete all values in the persistences table) and I am on something.blade.php and click a link that triggers a post request (please see the code snippet below), I will get forwarded to the login page. After login, I get following Laravel error:

Symfony \\ Component \\ HttpKernel \\ Exception \\ MethodNotAllowedHttpException

Route in web.php :

Route::post('something/{xtype}/{xid}/execute/{yid}', 'MyController@execute');

Controller logic in MyController.php :

public function execute($xtype, $xid, $yid)
{
    // business logic
    return back();
}

View something.blade.php :

<form action="/something/{{ $something->xtype }}/{{ $something->xid }}/execute/{{ $others->yid }}" method="POST" id="y_{{ $others->yid }}">
    {!! csrf_field() !!}
</form>

<a type="button" href="#" onClick="document.getElementById('y_{{ $others->yid }}').submit()">

Middleware AdminMiddleware.php :

public function handle($request, Closure $next)
{
    if (Sentinel::check())
    {
        if (Sentinel::getUser()->roles()->first()->slug == 'admin')
        {
            return $next($request);
        }
    }

    return redirect()->guest('/login')
    ->with(['error' => "You do not have the permission.."]);
    }
}

Edit:
After login, I will run into the LoginController and following code will be executed:

return redirect()->intended($fallbackUrl);

Since I am still new to Laravel, it is hard for me to debug deep inside the framework. Any ideas/suggestions from your side?
Everything is welcome! Thanks in advance!

You get this error when you try to post to an url that is setup for get requests or the other way around. You need to check in that direction!

On your form you didn't specify a method while on your routes you specified what type of the method your route should use

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