Error pages Symfony2 with locale - php

I'm trying to translate 404 pages on Symfony2, but it's not working. With 500 error pages it works great.
For example, when i try to access the page: /es/not-found it shows the 404 page, but when i access /en/not-found, it gives me the SAME page, although i have translated the error message on my messages-en.yml.
It seems it's always accessing the messages-es.yml file, because i've configured Symfony with this default locale (es).
If i print {{ app.request.locale }} on my Twig template, it doesn't give me anything.
I cleared the cache several times, yes :).
Thanks in advance!

If the localization configuration is properly working, you should be able to print the locale on Twig. With something like this:
{% if app.request.locale == 'en' %}
English 404 message
{% else %}
Other 404 message
{% endif %}
If your Symfony version is less than 2.1 you must use app.session.locale instead of app.request.locale
Nevertheless is a good practice to always create custom 404 and any other error page you need:
http://symfony.com/doc/current/cookbook/controller/error_pages.html
The most powerfull way is using this method:
Replace the default exception controller twig.controller.exception:showAction with your own controller and handle it however you want (see exception_controller in the Twig reference). The default exception controller is registered as a service - the actual class is Symfony\Bundle\TwigBundle\Controller\ExceptionController.
This way you can even have different templates for each country.
Kind regards.

Related

Symfony 3 - FOSUserBundle - register.html.twig - template not found

I'm pretty new to Symfony development and i just tried to use the FOSUserBundle. I followed the guide Getting Started With FOSUserBundle on the official Symfony website.
Afterwards i wanted to test the functionality of the installation and visited the link www.linktomytestproject.dev/login which worked flawlessly. But when i tried to visit www.linktomytestproject.dev/register to test the registration functionality, i got the following errormessage:
Unable to find template "register_content.html.twig" (looked into: /home/vagrant/mytestproject/app/Resources/views, /home/vagrant/mytestproject/vendor/symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form) in #FOSUser/Registration/register.html.twig at line 4.
500 Internal Server Error - Twig_Error_Loader
I checked the file register.html.twig, which contains the following code:
{% extends "#FOSUser/layout.html.twig" %}
{% block fos_user_content %}
{% include "register_content.html.twig" %}
{% endblock fos_user_content %}
When I change the third line into:
{% include "FOSUserBundle:Registration:register_content.html.twig" %}
The file is found correctly and i can use the registration functionality as intended.
But the change i made is inside the vendor folder, which obviously isn't affected by any means of version control. So i guess there has to be a place somewhere in the configuration files, where this file/foldermapping is affected.
I would really appreciate it, if someone could help me out with this question, even though this is probably a really basic problem.
First override the FOSUserBundle http://symfony.com/doc/current/bundles/FOSUserBundle/overriding_controllers.html
After that, you can create view folders like in vendor and you can use views like that;
{% include "YourBundle:Registration:register_content.html.twig" %}
It's a bug a issue is open on github
Is a bundle issue, in my project I change on the file
\vendor\friendsofsymfony\user-bundle\Resources\views\Registration\register.html.twig
this
{% include "register_content.html.twig" %}
for this
{% include "#FOSUser/Registration/register_content.html.twig" %}

A NULL check in Twig Template is not working which leads to an error (very strange behavior)

I’m currently facing a very strange problem in a Twig template (Symfony v2.8.9; Twig v1.24.1). The basic outline is: I’m trying to overwrite a block in a child template when a specific condition is met.
Here’s a snippet of my template:
{% extends "#App/search/resultList.html.twig" %}
{% if category.teaser %}
{% block description %}{{category.teaser.doSomething()}}{% endblock %}
{% endif %}
This code leads to the following error:
Impossible to invoke a method ("doSomething") on a null variable in #App/search/categoryResult.html.twig
The teaser property of the category indeed is null, which is okay and what the check is for.
I also tried an explicit {% if category.teaser is not null %} which also didn’t work and resulted in the same error.
Now for the really weird part (or maybe I’m just not seeing it why this wouldn’t be weird):
If I change the code to
{% block description %}
{% if category.teaser %}
{{category.teaser.doSomething()}}
{% endif %}
{% endblock %}
it works as intended (mostly, at least, because the parent block will always be overwritten).
This error seems to happen only in the Symfony dev environment.
It seems:
# Twig Configuration
twig:
debug: true
strict_variables: true
has something to do with it, which explains the dev environment only. If both values are set to false it works correctly.
Has anybody ever encountered a problem like this and solved it? Any help in solving this issue would be much appreciated.
As far as I know, it is not possible to conditionally override a block in twig, see also How can I conditionally override a TWIG layout block?
You have done the best practice by testing whether the variable is null or not.
In many other languages you can’t do something on a null. You can use the dump() function to check your variables.

render(controller()) throws Fatal error: Call to a member function getRelativePath() on a non-object

I've call it in common template for render google plus signin button, and this error do not appear only on homepage http://mydomain.local/ but when I go to any another page like http://mydomain.local/somepage this error appear.
any ideas what is that can be?
{{ render(controller('MyNamespaceMyBundle:MyController:loginButton')) }}
public function loginButtonAction()
{
return $this->render('MyNamespaceMyBundle:MyController:button.html.twig',array(
'client_key' => $this->config['client_key']
));
}
that's it, what else code you need?
I had the same problem. I was trying to render a twig template from a page that was included in the sonata_page.ignore_route_patterns, embedding a controller:
{% render(controller('AcmeDemoBundle:Controller:action', {param1 : 'value1'})) %}
And got the same exception, Call to a member function getRelativePath() on a non-object.
Sonata\PageBundle\Twig\Extension\PageExtension's controller function is checking if it's defined a pathInfo attribute, if not, it's retrieved from the relative path of the site selector. Since my route was excluded from sonata_page, it couldn't. Hence the exception.
So basically, I just passed the required attribute and problem solved:
{% set pathInfo = path(app.request.attributes.get('_route'),
app.request.attributes.get('_route_params')) %}
{% render(controller('AcmeDemoBundle:Controller:action',
{param1 : 'value1', pathInfo: pathInfo})) %}
It's revealed in the comments that you are trying to utilize Sonata's Page Bundle. I found that others have experienced this issue. According to the discussion thread you have either:
Not configured a site
Configured the site with a "bad host"
Make sure you follow the steps outlined in the Getting started docs. Specifically, it seems you probably need to execute this command:
php app/console sonata:page:create-site

No authentication token when another controller action is rendered in the template

I have a template which contains {{ render(controller(...) }} code. It works fine until I try to process 404 error. I'm getting The security context contains no authentication token trying to invoke controller action even though the result it returns contains single line of text and doesn't use session or anything.
Check if you rendered template (not only the subrequest, but the container and additional includes aswell) contains any references to {{ app.user }}. Quoting from the official documentation:
You must not use is_granted in your error pages (or layout used by
your error pages), because the router runs before the firewall. If the
router throws an exception (for instance, when the route does not
match), then using is_granted will throw a further exception. You can
use is_granted safely by saying {% if app.user and is_granted('...')
%}.
http://symfony.com/doc/current/cookbook/controller/error_pages.html

How to pass an array from Symfony 2 controller to a TWIG template ?

I can`t pass an array from a symfony 2 controller to a TWIG template. I use this code in the controller:
$searchTerms['color'] = "Red";
return $this->render('TestBundle::search.html.twig',
array(
"searchTerms" => $searchTerms));
In the twig template, I am trying to access the variable like this:
{{ searchTerms['color'] }}
{{ searchTerms.color }}
Both output nothing, empty string, so it seems like array comes to template but its elements are empty.
What`s wrong?
Yes, this code works. The first thing to check is that your twig code is in the correct page (TestBundle::search.html.twig). This might sound silly but that happens sometimes...
If this is all good, I suggest that you try to debug within your template. Debugging is the most important thing. You will always have this kind of problem while programming, especially when you try something new. The better you are at debugging your code, the better you are as a programmer because there is no way you can get everything right the first time.
So, how can you debug?
To debug within your twig template, you can use the debug extension of twig. To activate the debug option, you will have to do a quick change in your config file. You can also read this thread if your lost.
You can debug any variable within your template like this:
<pre>
{% debug searchTerms %}
</pre>
This way, you can easily debug your variable and test what your problem is:
{% debug searchTerms['color'] %}
If you want to debug things quickly, I highly recommend that you use the LadyBugBundle. It is an awesome tool that will allow you to do something like that:
In your controller:
ladybug_dump($searchTerms);
In your TWIG template:
{{ searchTerms|ladybug_dump }}
Not that different from a classic var_dump option, but if you have long arrays or objects, ladybug will impress you. More importantly, in a controller, you will often have the need to stop the code at a certain point to avoid the page to load after your debug statement, this is fairly easy with ladybug:
ladybug_dump_die($searchTerms);
You can even ask ladybug to load the "debugged" variable into Symfony profiler with this simple statement.
$this->get('ladybug')->log($searchTerms);
You have now direct access of the variable from a tab of the Symfony2 profiler.
Ladybug can do a lot more, but for this, the doc is really good.
I think you must change template like this:
{% for item in searchTerms %}
{{ item.color }}<br/>
{% endfor %}
See official documentation: Creating and using Templates->embedding-controllers

Categories