-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Document how to embed a controller as a service #8039
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -98,4 +98,36 @@ string syntax for controllers (i.e. **bundle**:**controller**:**action**): | |
) ?> | ||
</div> | ||
|
||
If your controller should be used :doc:`as a service </controller/service>`, | ||
you can reference it like this instead: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So this is interesting :). Starting in Symfony 3.3, the Given that, what made you create this PR? Were you seeing different behavior or were you confused by something? Cheers! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried helping @marcverney set it up on via Slack, but it did not work for him. @marcverney , can you comment on your setup and on the error message you were getting? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here it goes. I was trying to embed a controller action in a twig template, this way:
Here is my controller action:
And the CategoryRepository service definition
When I tried to access the page, I got the following error:
I then thought that maybe injecting a service in a controller action was not supported for subrequests (as I think that's what
This worked and my I think this sums it all up. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, and as for my setup, two things:
|
||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: html+twig | ||
|
||
{# app/Resources/views/base.html.twig #} | ||
|
||
{# ... #} | ||
<div id="sidebar"> | ||
{{ render(controller( | ||
'AppBundle\\Controller\\ArticleController:recentArticlesAction', | ||
{ 'max': 3 } | ||
)) }} | ||
</div> | ||
|
||
.. code-block:: html+php | ||
|
||
<!-- app/Resources/views/base.html.php --> | ||
|
||
<!-- ... --> | ||
<div id="sidebar"> | ||
<?php echo $view['actions']->render( | ||
new \Symfony\Component\HttpKernel\Controller\ControllerReference( | ||
'AppBundle\\Controller\\ArticleController:recentArticlesAction', | ||
array('max' => 3) | ||
) | ||
) ?> | ||
</div> | ||
|
||
|
||
The result of an embedded controler can also be :doc:`cached </http_cache/esi>` |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can anyone tell why these double backslashes are required?