I've been trying to get a custom 500 error page setup on a project that I'm working on but can't seem to get it to tie in with the combination of templating and theming I'm using.
I've got a custom 404 page by using the routes config file to redirect to a controller which then gets the template and view to load.
I was wondering if it's possible to do a similar thing with a 500 page? The reason I ask is because at the moment it just loads the '500.php' file in my view folder but ideally I want it to redirect to a controller so I can get a different 500 page depending on the theme that's currently set.
I've also used templates for the header, content and footer (but I don't think I'd be able to include the header and footer on the page as it relies on DB access). So all I'm really after is the ability to load a different 500 page depending on the theme so I can include some branding etc. which would differ for each one.
I was thinking of hard-coding the 500 page and just having switch statement which look at the current theme and then printing a different page dependent on that. But this would be hard to maintain so was hoping there'd be a better way.
Thanks for any help and apologies if this isn't explained very well!
The framework itself only deals with a 404, and it does that using an exception.
A 404 situation throws a HTTPNotFoundException, which is caught in your index.php, and which then does a new request for the 404 route. You can do the same for any other HTTP status.
would i have to wrap this in another try/catch statement to first check for HttpServerErrorException and then if there is one, route to a controller, and if not, carry on?
No, you can define multiple catch sections in a single try, so just add
catch (HttpServerErrorException $e) { // do your thing here }
You can use the same logic. It does a manual lookup for the route (you could use "_500_" if you want), and request it if it exists. If no route exists, it re-throws the exception. You should do that last bit, if you just carry on, you'll end up with a blank page, because the exception is caught but not handled.
What is "the default one"? The "500.php" view file?
Your code looks ok, and I'm sure nothing in the framework catches this Exception. The default view is rendered when it isn't caught, in which case the exception handler will call the response() method of the exception, which loads the 500 view...