Skip to content

Commit 05e4f3d

Browse files
author
Grégoire Paris
committed
Document how to embed a controller as a service
1 parent 32d878b commit 05e4f3d

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

controller/service.rst

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,38 @@ syntax:
7171
You cannot drop the ``Action`` part of the method name when using the
7272
single colon notation.
7373

74+
Embedding your controller in a template
75+
---------------------------------------
76+
77+
If you want to embed your controller in a template, you need to use the
78+
following syntax:
79+
80+
.. configuration-block::
81+
82+
.. code-block:: html+twig
83+
84+
{# app/Resources/views/base.html.twig #}
85+
86+
{# ... #}
87+
<div id="sidebar">
88+
{{ render(controller(
89+
'AppBundle\Controller\HelloController:indexAction',
90+
)) }}
91+
</div>
92+
93+
.. code-block:: html+php
94+
95+
<!-- app/Resources/views/base.html.php -->
96+
97+
<!-- ... -->
98+
<div id="sidebar">
99+
<?php echo $view['actions']->render(
100+
new \Symfony\Component\HttpKernel\Controller\ControllerReference(
101+
'AppBundle\Controller\HelloController:indexAction'
102+
)
103+
) ?>
104+
</div>
105+
74106
.. _controller-service-invoke:
75107

76108
Invokable Controllers

templating/embedding_controllers.rst

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,36 @@ string syntax for controllers (i.e. **bundle**:**controller**:**action**):
9898
) ?>
9999
</div>
100100

101+
If your controller should be used as a service, you can reference it like that
102+
instead:
103+
104+
.. configuration-block::
105+
106+
.. code-block:: html+twig
107+
108+
{# app/Resources/views/base.html.twig #}
109+
110+
{# ... #}
111+
<div id="sidebar">
112+
{{ render(controller(
113+
'AppBundle\Controller\ArticleController:recentArticlesAction',
114+
{ 'max': 3 }
115+
)) }}
116+
</div>
117+
118+
.. code-block:: html+php
119+
120+
<!-- app/Resources/views/base.html.php -->
121+
122+
<!-- ... -->
123+
<div id="sidebar">
124+
<?php echo $view['actions']->render(
125+
new \Symfony\Component\HttpKernel\Controller\ControllerReference(
126+
'AppBundle\Controller\ArticleController:recentArticlesAction',
127+
array('max' => 3)
128+
)
129+
) ?>
130+
</div>
131+
132+
101133
The result of an embedded controler can also be :doc:`cached </http_cache/esi>`

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy