0

I am trying to create custom exception pages in Symfony 2, and am following the Symfony 2 guide here and here. The second link says to modify the controller found Symfony\\Bundle\\TwigBundle\\Controller\\ExceptionController::showAction, but I can only find that file in the vendor\symfony\symfony\src\Symfony\Bundle\TwigBundle\Controller folder. From my understanding, I should never modify anything in the vendors folder.

I am wondering where is the ExceptionController I should be modifying located?

7
  • You don't want to modify any vendor files. You want to create a new ExceptionController. I answered a similar question on how to modify ExceptionControllers here: stackoverflow.com/questions/14282690/… Commented Jan 14, 2013 at 17:29
  • @Mike So do I ignore the ExceptionController inside the Vendor directory? Commented Jan 14, 2013 at 17:32
  • Can you give me more details on what you want to accomplish? Do you simply need to modify the HTML/twig templates? Is there custom PHP code you need to execute before displaying the exception templates? Commented Jan 14, 2013 at 17:33
  • @Mike I need to modify a twig/html template to display a 404 and 500 error page. There should not be any custom PHP code that needs to be executed prior to displaying the exception page. Commented Jan 14, 2013 at 17:38
  • For the config.yml, I am wondering if you are talking about the services.yml inside the config folder? Commented Jan 14, 2013 at 17:43

4 Answers 4

1

If you need to override the symfony error templates, take the templates from

vendor/symfony/src/Symfony/Bundle/TwigBundle/Resources/views/Exception

and place the ones you want to customize in a app/Resources/TwigBundle/views/Exception directory. Then customize as needed.

If you want to render templates based on the application, see my answer @ customize 403 error page depening on route in symfony 2.0

Sign up to request clarification or add additional context in comments.

9 Comments

Would I be putting my error pages in app/Resources/TwigBundle/views/Exception?
I also don't understand the if statement in your code. Does the if mean if they enter in that route, redirect them to a backend template? Sorry, I am new to PHP and frameworks.
Based on what? Implementing the solution in the other post? If you're using the solution in the other post, you can put them anywhere. The app/Resources/TwigBundle/views/Exception would primarily be if you only want to customize the error templates, and not override the exception controller.
@Jon -- If you are new to PHP, Symfony2 is going to be a tough thing to comprehend. I suggest reading the documentation from top to bottom to get a clear understanding of what's going on. Error templates are going to be the least of your issues. :)
Sorry, I should have explained myself better. I would like to override the exception controller as I am using your solution from your other post. I am currently learning PHP/Symfony on my own time, but at the same time I need to help my team on the project.
|
1

If you only need to change the layout of the error pages, creating the files on app/Resources/TwigBundle/views/Exception should be enough.

But if you need to pass additional parameters to your error pages you should override the default ExceptionController. To do that, you should create a new controller that extends the default one:

use Symfony\Bundle\TwigBundle\Controller\ExceptionController;
class MyExceptionController extends ExceptionController {
    //override the showAction() or findTemplate() method here
}

Then you need to tell symfony to use your new controller. This can be done by adding a new parameter on your parameters.yml file:

parameters:
    twig.controller.exception.class: YourBundle\Controller\MyExceptionController

1 Comment

Tried this but getting an exception: PHP Fatal error: Uncaught exception 'Guzzle\Http\Exception\ClientErrorResponseException' with message 'Client error response [status code] 404 [reason phrase] Not Found
0

For me it wasn't

parameters:
twig.controller.exception.class: YourBundle\Controller\MyExceptionController

but

parameters:
twig.controller.exception.class: YourNamespace\YourBundle\Controller\MyExceptionController

Comments

0

With Symfony 4 you need this:

class TwigExceptionControllerCompilerPass implements CompilerPassInterface
{
    public function process(ContainerBuilder $container)
    {
        $definition = $container->getDefinition('twig.controller.exception');
        $definition->setClass(TwigExceptionController::class);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.