【问题标题】:Laravel 5.6 MethodNotAllowedHttpException when user is logged out but sending a post requestLaravel 5.6 MethodNotAllowedHttpException 用户注销但发送发布请求时
【发布时间】:2018-08-01 08:15:24
【问题描述】:

我正在使用 Sentinel 进行身份验证。只要我登录,一切正常。但是当我注销时(删除persistences 表中的所有值)并且我在something.blade.php 上并单击触发发布请求的link(请参阅下面的代码sn-p),我会得到转发到登录页面。登录后,我收到以下 Laravel 错误:

Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException

路由在web.php

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

MyController.php 中的控制器逻辑:

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

查看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()">

中间件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.."]);
    }
}

编辑:
登录后,我会遇到LoginController,然后会执行以下代码:

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

由于我还是 Laravel 的新手,我很难在框架内部进行深入调试。您有什么想法/建议吗?
欢迎一切!提前致谢!

【问题讨论】:

    标签: php laravel laravel-5 cartalyst-sentinel


    【解决方案1】:

    当您尝试发布到为获取请求或其他方式设置的 url 时,您会收到此错误。你需要检查那个方向!

    在您的表单上您没有指定方法,而在您的路线上您指定了您的路线应使用的方法类型

    【讨论】:

      猜你喜欢
      • 2021-07-25
      • 1970-01-01
      • 2019-01-20
      • 2017-05-23
      • 2018-10-08
      • 2019-03-05
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      相关资源
      最近更新 更多