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.
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);
}
@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.
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.