Customize template symfony2 exception - php

I search in the forum and I not found nothing.
I have a problem with the exceptions handler on Symfony2. When I try to access a route that it not exists, Symfony retrieves a blank page (in prod environment). The log writes a message: Route not exists for.... but the custom template is not shown.
In config.yml:
twig:
exception_controller: twig.controller.exception:showAction
And the templates is in:
app/Resources/TwigBundle/views/Exception/error500.html.twig
app/Resources/TwigBundle/views/Exception/error404.html.twig
What do I do wrong?

Related

How to change default twig error templates in Symfony 4?

I am building an app using Symfony 4.4. I am trying to render custom 404 error page. I followed the documentation and created error.html.twig and error404.html.twig files in templates/bundles/TwigBundle/Exception. However I still get the 'Oops! An Error Occurred' with code 500 page that is by default from symfony.
Can someone point what can have I done wrong?
In dev mode I see that the app throws NotFoundHttpException but in prod mode I always get the page with code 500 even if I visit an undefined route.
This is what I get when I visit an undefiend route:
500 error
You can use the built-in Twig error renderer to override the default error templates. Both the TwigBundle and TwigBridge need to be installed for this
When the error page loads, Symfony\Bridge\Twig\ErrorRenderer\TwigErrorRenderer is used to render a Twig template to show the user.
This renderer uses the HTTP status code and the following logic to determine the template filename:
Look for a template for the given status code (like error500.html.twig);
If the previous template doesn’t exist, discard the status code and look for a generic error template (error.html.twig).
To override these templates, rely on the standard Symfony method for overriding templates that live inside a bundle and put them in the templates/bundles/TwigBundle/Exception/ directory.

render php file return template ::/var/www/vhosts/ does not exist

I am using Plesk for my project. It works fine when I render a twig file but when I render a php file in my Controller an error occurred:
"The template "::/var/www/vhosts/domain/httpdocs/project/templates/test.php" does not exist."
This is my code:
$this->render('/var/www/vhosts/domain/httpdocs/project/templates/test.php');
$this->render('/var/www/vhosts/domain/httpdocs/project/templates/test.php');
The above code is wrong in symfony. render function (used in the controller) is used for rendering twig file content. The above code gives the result like that
No engine is able to work with the template "/var/www/vhosts/domain/httpdocs/project/templates/test.php"
I added in framework.yaml templating: engines: ['twig', 'php'] So that I can reder a php file as mention in the documentation https://symfony.com/doc/current/templating/PHP.html

Symfony 4 set Twig template path in controller

I searched everywhere but unfortunately without result. The case is as following:
I'm using a general Symfony 4 codebase with basic TWIG template files. Next to that i have multiple domains who refer to the codebase, because of that reason I want to set the path to my TWIG files in the controller from my codebase:
return $this->render("path/to/domain/and/twig/temp", "domain variables");
This is not working and I can't find how to change my TWIG path withing my controller. Can somebody help with this?
Thanks!
Symfony uses app/Resources/views/ directory for templates. If you want to change you can configure it in config.yml as below.
twig:
# ...
paths: ["%kernel.project_dir%/templates"]

Symfony 3: Customize the template for #Secutity annotation exceptions

I want to customize the twig template for errors thrown on #Security("has_role('CUSTOM_ADMIN')")
I have already seen a similar issue here but for Symfony 2.
I think that #Security annotation logic bypasses the Twig template overriding because I have followed the directions on the official docs about customizing error pages but those template files are getting ignored and i just get the default Symfony exception page with:
Expression "has_role('CUSTOM_ADMIN')" denied access.
Am I missing something?
While you're in the development environment, Symfony shows the big exception page instead of your shiny new customized error page. So, how can you see what it looks like and debug it?
Fortunately, the default ExceptionController allows you to preview your error pages during development.
To use this feature, you need to have a definition in your routing_dev.yml file like so:
# app/config/routing_dev.yml
_errors:
resource: "#TwigBundle/Resources/config/routing/errors.xml"
prefix: /_error
If you're coming from an older version of Symfony, you might need to add this to your routing_dev.yml file. If you're starting from scratch, the Symfony Standard Edition already contains it for you.
With this route added, you can use URLs like:
http://localhost/app_dev.php/_error/{statusCode}
http://localhost/app_dev.php/_error/{statusCode}.{format}
to preview the error page for a given status code as HTML or for a given status code and format.
Ref: http://symfony.com/doc/current/controller/error_pages.html#testing-error-pages-during-development

Creating custom error page in Symfony 2.5

I am working with Symfony 2.5.3 and I am trying to create the custom error page so for example if someone lands on a page or try to access a page that does not exist the 404 error message should appear within the theme of the site.
I understand that default 404 error page sits inside the TwigBundle that comes with symfony and looking at some other answers on this site people are suggesting that in order to have the custom error page I need to copy them in directory ROOT app/Resources/TwigBundle/views/Exception so I copied over the error.html.twig and even created error404.html.twig but this does not work. I also looked into this solution but still no luck.
I tried to access my development site via http://127.0.0.1:8000/app.php/services to check may be in production i will see the correct template but I dont, please note that i have cleared the cache before accessing http://127.0.0.1:8000/app.php/services and I still see the default error template that comes with Symfony
I will really appreciate any help on this.
First double check this :
Check if you're custom error template is in app/Resources/TwigBundle/views/Exception folder
Then double check the name of you're custom error template if it is error.html.twig OR/AND error404.html.twig
Then create the custom error/template that you want users to see
You can see the custom error/page when you view the website via app.php which is a production environment.
Beetween each check
rm -rf app/cache/*
It should works now ;)
Another way to easily test your custom error template in dev mode is to use instead WebfactoryExceptionsBundle who provide a test controller for displaying your custom error page even when kernel.debug is set to true.

Categories