Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Custom 500 error page with templates and themes?
  • 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.
  • so the code in index.php which does this is:

    try
    {
        $response = Request::forge()->execute()->response();
    }
    catch (HttpNotFoundException $e)
    {
        $route = array_key_exists('_404_', Router::$routes) ? Router::$routes['_404_']->translation : Config::get('routes._404_');

        if($route instanceof Closure)
        {
            $response = $route();
           
            if( ! $response instanceof Response)
            {
                $response = Response::forge($response);
            }
        }
        elseif ($route)
        {
            $response = Request::forge($route, false)->execute()->response();
        }
        else
        {
            throw $e;
        }
    }


    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?
  • HarroHarro
    Accepted Answer
    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.
  • Awesome thanks for letting me know, never knew you could have multiple catch statement, thanks again!
  • I added the following to index.php
    catch (HttpServerErrorException $e)
    {
    // Add support for custom 500 pages
    $route = array_key_exists('_500_', Router::$routes) ? Router::$routes['_500_']->translation : Config::get('routes._500_');

    if($route instanceof Closure)
    {
    $response = $route();

    if( ! $response instanceof Response)
    {
    $response = Response::forge($response);
    }
    }
    elseif ($route)
    {
    $response = Request::forge($route, false)->execute()->response();
    }
    else
    {
    throw $e;
    }
    }

    and in routes.php
    '_500_'   => 'welcome/500',   

    But custom 500 page is not displayed. Only the default one.
  • 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...

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion