shaneparsons's avatar

Custom 403 - Lumen

I'm having problems trying to show a custom 403 page with lumen – the problem sounds like it's very similar to this one.

I have a custom 404 working perfectly, and I've added the "403 code" (below) to the render function of app/Exceptions/Handler.php, but the exception is never caught.

if ($e instanceof AccessDeniedHttpException)
{
    return response(view('errors.403'), 403);
}

Any help would be greatly appreciated.

0 likes
2 replies
shaneparsons's avatar
shaneparsons
OP
Best Answer
Level 1

There definitely wasn't anything straight forward about getting here, but adding 403 stuff to vendor/laravel/lumen-framework/src/Concerns/RegistersExceptionHandlers.php seems to have finally gotten it working.

Here's the modified code, just incase anyone else stumbles into this issue.

mooseh's avatar

I think this is a way nicer way to handle in app/Exceptions/Handler.php

        $environment = app()->environment();

        //production only
        if(in_array($environment, ["prod", "production"]) && !$request->wantsJson()){
            if ($e instanceof HttpException){
                return response(view("errors.{$e->getStatusCode()}"), $e->getStatusCode());
            }
        }

Please or to participate in this conversation.