Laravel返回HttpException对象而不是显示自定义错误页面

Sty*_*hon 4 php error-handling laravel-5.2

我正在使用spatie权限模块来控制网站中的角色和权限。我在身份验证中间件中添加了一些内容。我的句柄现在看起来像这样:

public function handle($request, Closure $next, $guard = null)
{
    if (Auth::guard($guard)->guest())
    {
        if ($request->ajax() || $request->wantsJson())
            return response('Unauthorized.', 401);

        return redirect()->guest('login');
    }

    if ( ! Auth::user()->can('access acp') )
    {
        if ($request->ajax() || $request->wantsJson())
            return response('Unauthorised.', 403);

        abort(403, "You do not have permission to access the Admin Control Panel. If you believe this is an error please contact the admin who set your account up for you.");
    }

    return $next($request);
}
Run Code Online (Sandbox Code Playgroud)

因此,如果用户未登录,我们会将其发送到登录页面,否则我们将检查是否具有访问acp的权限,如果没有,则向他们显示403错误。我已将403.blade.php添加到views / errors文件夹。但是,当我运行该代码时,我只会大呼一声!并且开发人员工具显示将退还500 ISE。我不明白为什么看不到自定义错误页面。

到目前为止,我已经尝试将环境切换到生产环境并关闭调试模式,但是该页面没有显示。我也尝试过抛出授权异常,但这没有什么不同。我也尝试使用,App::abort()但是我仍然得到了500 ISE。

我已经尝试使用Google搜索该问题,但找不到其他人遇到此问题。我真的很感激任何帮助。

哎呀回来了

错误输出

如果我这样修改代码

try
{
    abort(403, "You do not have permission to access the Admin Control Panel. If you believe this is an error please contact the admin who set your account up for you.");
} catch ( HttpException $e )
{
    dd($e);
}
Run Code Online (Sandbox Code Playgroud)

然后我得到HttpException带有错误代码和消息的实例,那么为什么不显示自定义错误页面呢?

Mic*_*eda 5

我设法通过下面的代码解决了这个问题(请注意,这是一个Lumen应用程序,但它应与Laravel一起使用)

routes.php

$app->get('/test', function () use ($app) {
    abort(403, 'some string from abort');
});
Run Code Online (Sandbox Code Playgroud)

资源/视图/错误/403.blade.php

<html>
    <body>
    {{$msg}}
    <br>
    {{$code}}
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

app / Exceptions / Handler.php,如下修改render()函数

public function render($request, Exception $e)
{
    if ($e instanceof HttpException) {
        $statusCode = $e->getStatusCode();

        if (view()->exists('errors.'.$statusCode)) {
            return response(view('errors.'.$statusCode, [
                'msg' => $e->getMessage(), 
                'code' => $statusCode
            ]), $statusCode);
        }
    }

    return parent::render($request, $e);
}
Run Code Online (Sandbox Code Playgroud)

它完成了Laravel根据文档所做的工作