TheFox's avatar

Laravel 5 Customize 403 Error Page

Hi,

how can I customize the 403 error page in Laravel 5?

I created a file resources/views/errors/403.blade.php (and also a twig file because I use TwigBridge) but when authorize() in a FormRequest returns false it doesn't take the template. It only shows Forbidden.

All other error page templates are taken correctly.

0 likes
15 replies
bashy's avatar
bashy
Best Answer
Level 65

Override the forbiddenResponse() method?

public function forbiddenResponse()
{
    return response()->view('errors.403');
}
4 likes
TheFox's avatar

To overwrite forbiddenResponse() was a good hint. But it doesn't work with your code:

ErrorException in HttpResponseException.php line 21:
Argument 1 passed to Illuminate\Http\Exception\HttpResponseException::__construct() must be an instance of Symfony\Component\HttpFoundation\Response, instance of Illuminate\View\View given, called in vendor/laravel/framework/src/Illuminate/Foundation/Http/FormRequest.php on line 120 and defined

So it accutally works with this code:

public function forbiddenResponse(){
    return Response::make(view('errors.403'), 403);
}
1 like
TheFox's avatar

@bestmomo But shouldn't forbiddenResponse() always return a Symfony\Component\HttpFoundation\Response instance, as described in my comment above?

bestmomo's avatar

@TheFox just override like that :

public function failedAuthorization()
{
    abort(403);
}

The response will have 403 code (forbidden) and your view error.403

TheFox's avatar

@bestmomo Yes, I see: 403 is not the same as error.403; --- 403 != error.403. But I wonder why this is only on 403 errors an exception. On 404, 503, etc - I doesn't need to declare it explicitly that the templates are used for each error code. And also the files have all the same naming convention: NUMBER.blade.php - not error.NUMBER.blade.php.

It doesn't make sense to me that I need to declare it only on 403 errors explicitly in a own function and not all other error codes.

bestmomo's avatar

@TheFox sorry I mean only 403 and not error.403 for the file, as all others in errors folder.

bashy's avatar

Yeah sorry was typing it quickly so forgot you needed the response

TheFox's avatar

Thank you for your help. :)

In Illuminate\Foundation\Http\FormRequest why doesn't the function forbiddenResponse() use the exception handler Illuminate\Foundation\Exceptions\Handler?

public function forbiddenResponse()
{
    return new Response('Forbidden', 403);
}

It creates it's own Response with content Forbidden. But why? Why doesn't it use also the the Handler class that the template is used if it exists?

Every other error code (404, 503, etc.) uses the renderHttpException() function of the Handler class.

So in this case it's a good choice to overwrite forbiddenResponse() as @bashy pointed it out.

1 like
bestmomo's avatar

It's more logical to override failedAuthorization.

bashy's avatar

@bestmomo True, I didn't know what way they were showing this 403. Override both if you want 403 view to be shown on either of those :P

mbpp's avatar

hello, can someone tell me how would i make a a "abort(403);" and redirect to another page, i tried this example above, but didnt worked.

 protected function unauthorized()
    {
         abort(403, 'Unauthorized action.');
         return redirect('/dashboard');
    }

Please or to participate in this conversation.