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
Related
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
I need to override behavior of sylius_partial_product_latest route to have ability render template with additional parameter.
The simple example: at the different pages I need to show from 2 to 4 products at a time with carousel scrolling through 4-8 products. Now in the SyliusWebBundle:Frontend/Product:latest template I have:
{{ grid(products) }}
But I need in some cases:
{{ grid(products, 2) }}
In others:
{{ grid(products, 3) }}
{# or it can be 4 or any other number #}
Now in template I have only products var (it served by Resources mechanism), I can pass variables to the repository where I can add it to my products array, but it is not a good way. It can be accomplished by simple passing variables to the query:
{{ render(url('sylius_partial_product_latest', {'size': 3, 'limit': 2, 'template': 'SyliusWebBundle:Frontend/Product:latest.html.twig'})) }}
I need to have ability to pass variable size to template SyliusWebBundle:Frontend/Product:latest outside of products.
I tried override the ProductBundle and ResourceBundle... But overriding controllers with custom action (I do not want to override indexAction or showAction) led me nowhere. PHP doesn't hit my controller at all. Instead it hit showAction of ResourceBundle. And, yes, I overrode routing to point my controller.
If I override ProductController with new route and use it in my template it throws exception:
An exception has been thrown during the rendering of a template
("Controller "sylius.controller.product:partialAction" for URI
"/partial" is not callable.") in
SyliusWebBundle:Frontend/Product:show.html.twig at line 136.
When I change _controller to point my controller in standard Symfony manner I have next exception:
Catchable fatal error: Argument 1 passed to
Sylius\Bundle\ResourceBundle\Controller\ResourceController::__construct()
must be an instance of
Sylius\Bundle\ResourceBundle\Controller\Configuration, none given,
called in
/Users/mihail/Sites/magazin/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/ControllerResolver.php
on line 77 and defined in
/Users/mihail/Sites/magazin/vendor/sylius/sylius/src/Sylius/Bundle/ResourceBundle/Controller/ResourceController.php
on line 63
There are 2 ways that you could go with this..
Controller
You could extend the controller and pass the variable into your template the same way you could with any other controller.
Request
You could just pass the variable to your controller through the GET parameters as the request like your..
{{ render(url('sylius_partial_product_latest', {'size': 3, 'limit': 2, 'template': 'SyliusWebBundle:Frontend/Product:latest.html.twig'})) }}
.. and then get that in your template using the request object like..
{{ grid(product, app.request.get('size')) }} // with a default or null
I'm trying to customize 404 error page so I did:
1) Create app/Resources/TwigBundle/views/Exception/error404.html.twig
2) I changed is_granted() for {% if app.user and is_granted('...') %} in "layout.html.twig", used by "error404.html.twig"
3) php app/console cache:clear --env=prod --no-debug
4) Access in prod enviroment to http:/localhost/app.php and cause 404 error, getting a blank page instead of my custom error page.
I checked my "prod.log" file and I saw a NotFoundHttpException and this Twig error:
request.ERROR: Exception thrown when handling an exception
(Twig_Error_Runtime: An exception has been thrown during the rendering
of a template ("Unable to generate a URL for the named route "" as
such route does not exist.") in "::layout.html.twig" at line 33.) [] []
It appears that path(app.request.attributes.get('_route')) returns an empty value.
-> How can I get current route and make error page work?<-
Thanks
As Maerlyn says in his comment, it's likely that, if you're on a 404 page, there is no matching route.
However, you can retrieve useful information from the request object which may help you here:
{{ app.request.uri }}
will return you the full URL of the page the user was trying to access. And
{{ app.request.pathInfo }}
will return the path (starting with a slash).
You can find out more about the methods available on the request object by looking at /vendor/symfony/symfony/src/Symfony/Component/HttpFoundation/Request.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.
With Silex (the PHP micro framework), it's possible to give names to existing controllers, so that we can easily generate urls to them later. Example:
$app->get('/gallery', function () {...})
->bind('gallery');
// Later on, in a template
{{ path('gallery') }}
I think this is really useful and I can't live without it.
But is it possible to register a route to an external website ? Say I'd like to generate urls to a google search, kind of
{{ path('google', {'search':'symfony'}) }}
// Would render to http://google.com/search?q=symfony
I take any idea :) Thx for your help !
path() is a Twig Extension for routing. Routing is to route an incoming URL to a controller action.
You can, however, create your own twig extension if you want a helper to easily create those standard outgoing URLs.
Take a look at: http://symfony.com/doc/current/cookbook/templating/twig_extension.html
You could then create an extension that turns {{ google('search string') }} into a URL. Only the imagination is your boundary.