I am trying to have Laravel respond a custom 403 error rather than display the default nginx forbidden page.
I have custom handlers for most of my exception types in app/Exceptions/Handler.php
and they are working just fine, except for 403. I added the following code to the Handler.php file
within the render
function to handle 403:
if($e instanceof HttpException && $e->getStatusCode() == 403){
return response("403 Forbidden error response");
}
but I still just see the default nginx forbidden page if I lets say navigate to http://myapp.com/images/
At this point I am fairly certain this is an nginx config I may be missing, but am not 100% sure what and after searching the web can't seem to find the solution.
Thanks
Thanks for the advice and reviews everyone, I have been able to find and fix what was causing the issue. As Elias Soares mentioned above in comment, the issue was obviously happening before Laravel was even reached. I went into my nginx config file and noticed the following line:
error_page 404 /index.php;
and since Laravel was indeed correctly handling 404 errors, I simply added the exact same thing for 403:
error_page 403 /index.php;
and now Laravel is handling any 403 errors the way I wanted it to do.
Thanks!
You don't have to do anything special.
Just create 403.blade.php under resources/views/errors/
and you should be good.