Ka.
Ka.

Reputation: 1209

Error page based on host

With Symfony2, it's easy to match routes based on the host (details here http://symfony.com/doc/current/components/routing/hostname_pattern.html).

I would like to have different error pages according to host.

404 error on example.com -> one layout
404 error on test.com -> another layout

How would you do that ?

Upvotes: 2

Views: 118

Answers (1)

Andrea Sprega
Andrea Sprega

Reputation: 2271

If you have access to the configuration of your webservers, you could declare the template name as an environment variable which Symfony can pick up and use as application parameter.

Example: configure the following ENV in Apache, for the first (virtual)host:

SetEnv SYMFONY__ERROR_TEMPLATE SomeBundle:error:template1.html.twig

... and for the second:

SetEnv SYMFONY__ERROR_TEMPLATE SomeBundle:error:template2.html.twig

Then you can inject that parameter to whatever service you use to render your error page (most likely an exception listener):

# Your exception listener
your_exception_listener:
    class:     SomeClass
    arguments:
        - @dependency1
        - ...
        - %error_template%

You can then simply render the template passed as a parameter and remove any hardcoded template reference in code.

This way, by running the same code in both hosts, you can display two different layouts for error pages.

NOTE: You'll also have to remember setting an environment variable with the same name in your shell, or you will get exceptions when the app runs in CLI mode.

More information here: http://symfony.com/doc/current/cookbook/configuration/external_parameters.html

Upvotes: 2

Related Questions