Custom error page

With Sulu it is very easy to customize the error pages for your website users. You can define a template for each HTTP status code.

Configuration

The following code-block from the webspace configuration file shows a default configuration for the exception templates. If you want to add an own exception for example 400 you can simply add it to the list. You can specify that for each theme.

<templates>
    <template type="error">ClientWebsiteBundle:views:error.html.twig</template>
    <template type="error-404">ClientWebsiteBundle:views:error404.html.twig</template>
    <template type="error-500">ClientWebsiteBundle:views:error500.html.twig</template>
</templates>

The ExceptionController uses the status-code of the response to determine which template is responsible for the exception. If no special template is defined it uses the template without an error code.

Twig-Template

In the twig-template you can use your website master template to reuse your style.

{% extends "ClientWebsiteBundle:views:master.html.twig" %}

{% block title %}Error {{ status_code }}{% endblock %}

{% block content %}
    <h1>Error {{ status_code }}</h1>
    <p>{{ status_text }}</p>

    <p>{{ exception.message }}</p>
{% endblock %}

Warning

Be careful which variable you use in your master.html.twig. If you use variables which are not defined in the error-template, the error-page cannot be rendered.

Following variables are usable inside the exception template.

Name Description
status_code status_text exception currentContent urls request.webspaceKey request.defaultLocale request.locale request.portalUrl request.resourceLocatorPrefix request.resourcelocator request.get request.post request.analyticsKey http-status-code http-status-code message complete exception object repsonse content which were rendered bofore exception was thrown localized urls to start page (e.g. for language-switcher) key of the current webspace default locale of current portal current locale url of current portal prefix for resourcelocators of current portal current resourcelocator array of get parameter array of post parameter analytics key of current webspace

Test it

To test your error pages you can use following routes:

{portal-prefix}/_error/{statusCode}

Note

If you are not sure about your portal configuration you can get the routes with this app/webconsole router:debug | grep _error command

Examples:

sulu.lo/ch._twig_error_test       ANY    ANY    sulu.lo /ch/_error/{code}.{_format}
sulu.lo/en._twig_error_test       ANY    ANY    sulu.lo /en/_error/{code}.{_format}
sulu.lo/fr._twig_error_test       ANY    ANY    sulu.lo /fr/_error/{code}.{_format}
sulu.lo/de._twig_error_test       ANY    ANY    sulu.lo /de/_error/{code}.{_format}
sulu.lo._twig_error_test          ANY    ANY    sulu.lo /_error/{code}.{_format}