I create custom controller(extending Sonata\AdminBundle\Controller\CRUDController) and action.
When render this action {{render(controller('MainBundle:SonataAdmin/Order:searchCertificate'))}}
I get Symfony error:
An exception has been thrown during the rendering of a template ("There is no _sonata_admin defined for the controller MainBundle\Controller\SonataAdmin\OrderController and the current route").
I found answer in official documentation:
If you want to render a custom controller action in a template by
using the render function in twig you need to add _sonata_admin as an
attribute. For example; {{
render(controller('AppBundle:XxxxCRUD:comment', {'_sonata_admin':
'sonata.admin.xxxx' })) }}. This has to be done because the moment the
rendering should happen the routing, which usually sets the value of
this parameter, is not involved at all, and then you will get an error
"There is no _sonata_admin defined for the controller
AppBundleControllerXxxxCRUDController and the current route ' '."
i have solved this Problem by setting the _sonata_admin in the comming request:
with normal Controller:
$request->request->set('_sonata_admin','admin.template');
Related
Updating deprecated code and i have following problem with render tag
> {% render url('_internal_main_navigation', {}) %}
transaltes to
{{ render(controller('MyBundle::menu', {})) }}
Twig does this thing where it appends "Controller" and "Action" to the specified route so That should call MyBundle/Controller/Controller->menuAction() but apparently the Controller class is supposed to have some sort of prefix like "MyController" so the twig route can be specified like 'Bundle:My:menu'
otherwise i get
Class "MyBundle" noes not exist
Can i make it work without prefixing the controller?
I'd recommend to use render_esi() which supports routes via url() by name aswell and you're set for ESI in the future.
When using the default render() function (or setting the renderer to
inline), Symfony merges the included page content into the main one
before sending the response to the client. But if you use the esi
renderer (i.e. call render_esi()) and if Symfony detects that it's
talking to a gateway cache that supports ESI, it generates an ESI
include tag. But if there is no gateway cache or if it does not
support ESI, Symfony will just merge the included page content within
the main one as it would have done if you had used render().
See Using ESI in Symfony
The following syntax will work in your case:
{{ render_esi(url('_internal_main_navigation', {})) }}
If you'd like to render by controller-name, you could turn your controller into a service and refer to it by the name of the service (i.e. mybundle_controller):
{{ render_esi(controller('mybundle_controller:menuAction', {})) }}
I'm working on implementing a ROLE based admin application. I have a custom voter and at some point I'm doing something like:
if($role && VoterInterface::ACCESS_GRANTED !== $voteResult) {
throw new AccessDeniedException('Unauthorized access!');
}
and the result is that a custom error403.html.twig template is rendered.
So far so good.
The error403 template extends the main template in which at some point I'm building a menu using app.request.get('_route') for generating the links.
The problem is app.request.get('_route') is null.
xDebug-ing the issue I've noticed that somehow the $request->attributes->parameters array does not contain _route or _route_params keys.
Any thoughts?
The problem is Symfony uses sub-request for rendering error pages. It doesn't need a router and you have not exatly the same request object as in master request.
Github issue
https://github.com/symfony/symfony/issues/5804
Same question on SO
app.request.attributes.get('_route') is empty when I override 404 error page
Some theory
https://knpuniversity.com/screencast/symfony-journey/sub-request-internals
You can write your own exception listener and modify this behaviour in some way.
I try to render a controller in my webSite but an exception is triggered:
HERE MY CODE
render(controller("HWIOAuthBundle:Connect:login"))
HERE THE MESSAGE
An exception has been thrown during the rendering of a template ("Controller "HWIOAuthBundle:Connect:login" for URI "/_fragment" is not callable.") in MYBlogBundle::layout.html.twig at line 39
I do not understand because the route is the good one.
vendor/hwi/oauth-bundle/HWI/Bundle/OAuthBundle/Resources/Views/Connect/login.html.twig
There is no loginAction in HWIOAuthBundle's ConnectController. Please use connectAction, it will render vendor/hwi/oauth-bundle/HWI/Bundle/OAuthBundle/Resources/Views/Connect/login.html.twig template.
render(controller("HWIOAuthBundle:Connect:connect"))
I've created a twig template that will create a table of items with checkboxes and a js function that can be triggered using a button that will return the IDs of all items where the checkboxes are checked. This works fine so far. Now I need to call a service and pass an array of all selected IDs.
Is there a good way to call the service from within the js part in the twig template? I don't want to create a controller for the service and use curl to call it.
Best regards Christian
# app/config/config.yml
twig:
globals:
myService: "#my.service"
And in twig you can use now:
{{ myService.anyMethod() }}
You can also make Twig extension:
http://symfony.com/doc/current/cookbook/templating/twig_extension.html
on my Admin template I've used render function to add the menu.
The controller sidebar add all links from db.
The problem is made when i want to add "current" class because i can't access of current url/controller from a render request.
{{ render(controller('AdminDashboardBundle:Template:sidebar')) }}
How I can access to all informations from the render controller (without pass a var )?
Thanks
The RequestStack service has been built with Symfony 2.4. If you declare your Template controller as a service and inject RequestStack, you'll be able to use your current render call without passing arguments.
But you are speaking about Symfony 2.3, and unfortunately I don't think it is possible to do what you want without arguments. Here are some example on how to pass the current route / the URL as an argument of your controller.
1) Passing the URL :
{{
render(controller('AdminDashboardBundle:Template:sidebar', {
'url': app.request.requesturi
}))
}}
2) Passing the route :
{{
render(controller('AdminDashboardBundle:Template:sidebar', {
'route': app.request.attributes.get('_route'),
'route_params': app.request.attributes.get('_route_params')
}))
}}
I know you want to use Symfony2.3 and this call without passing vars, I think that's simply not possible because of how work scopes.