@@ -91,10 +91,9 @@ or a ``Closure``), in Symfony, a controller is usually a single method inside
91
91
a controller object. Controllers are also called *actions *.
92
92
93
93
.. code-block :: php
94
- :linenos:
95
94
96
- // src/Acme/HelloBundle /Controller/HelloController.php
97
- namespace Acme\HelloBundle \Controller;
95
+ // src/AppBundle /Controller/HelloController.php
96
+ namespace AppBundle \Controller;
98
97
99
98
use Symfony\Component\HttpFoundation\Response;
100
99
@@ -151,7 +150,7 @@ to the controller:
151
150
# app/config/routing.yml
152
151
hello :
153
152
path : /hello/{name}
154
- defaults : { _controller: AcmeHelloBundle :Hello:index }
153
+ defaults : { _controller: AppBundle :Hello:index }
155
154
156
155
.. code-block :: xml
157
156
@@ -163,7 +162,7 @@ to the controller:
163
162
http://symfony.com/schema/routing/routing-1.0.xsd" >
164
163
165
164
<route id =" hello" path =" /hello/{name}" >
166
- <default key =" _controller" >AcmeHelloBundle :Hello:index</default >
165
+ <default key =" _controller" >AppBundle :Hello:index</default >
167
166
</route >
168
167
</routes >
169
168
@@ -175,7 +174,7 @@ to the controller:
175
174
176
175
$collection = new RouteCollection();
177
176
$collection->add('hello', new Route('/hello/{name}', array(
178
- '_controller' => 'AcmeHelloBundle :Hello:index',
177
+ '_controller' => 'AppBundle :Hello:index',
179
178
)));
180
179
181
180
return $collection;
@@ -184,10 +183,10 @@ Going to ``/hello/ryan`` now executes the ``HelloController::indexAction()``
184
183
controller and passes in ``ryan `` for the ``$name `` variable. Creating a
185
184
"page" means simply creating a controller method and associated route.
186
185
187
- Notice the syntax used to refer to the controller: ``AcmeHelloBundle :Hello:index ``.
186
+ Notice the syntax used to refer to the controller: ``AppBundle :Hello:index ``.
188
187
Symfony uses a flexible string notation to refer to different controllers.
189
188
This is the most common syntax and tells Symfony to look for a controller
190
- class called ``HelloController `` inside a bundle named ``AcmeHelloBundle ``. The
189
+ class called ``HelloController `` inside a bundle named ``AppBundle ``. The
191
190
method ``indexAction() `` is then executed.
192
191
193
192
For more details on the string format used to reference different controllers,
@@ -202,7 +201,8 @@ see :ref:`controller-string-syntax`.
202
201
203
202
.. tip ::
204
203
205
- You can learn much more about the routing system in the :doc: `Routing chapter </book/routing >`.
204
+ You can learn much more about the routing system in the
205
+ :doc: `Routing chapter </book/routing >`.
206
206
207
207
.. index ::
208
208
single: Controller; Controller arguments
@@ -212,29 +212,29 @@ see :ref:`controller-string-syntax`.
212
212
Route Parameters as Controller Arguments
213
213
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
214
214
215
- You already know that the ``_controller `` parameter ``AcmeHelloBundle :Hello:index ``
215
+ You already know that the ``_controller `` parameter ``AppBundle :Hello:index ``
216
216
refers to a ``HelloController::indexAction() `` method that lives inside the
217
- ``AcmeHelloBundle `` bundle. What's more interesting is the arguments that are
218
- passed to that method::
217
+ ``AppBundle `` bundle. What's more interesting is the arguments that are passed
218
+ to that method::
219
219
220
- // src/Acme/HelloBundle /Controller/HelloController.php
221
- namespace Acme\HelloBundle \Controller;
220
+ // src/AppBundle /Controller/HelloController.php
221
+ namespace AppBundle \Controller;
222
222
223
223
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
224
224
225
225
class HelloController extends Controller
226
226
{
227
227
public function indexAction($name)
228
228
{
229
- // ...
229
+ // ...
230
230
}
231
231
}
232
232
233
233
The controller has a single argument, ``$name ``, which corresponds to the
234
234
``{name} `` parameter from the matched route (``ryan `` in the example). In
235
235
fact, when executing your controller, Symfony matches each argument of
236
- the controller with a parameter from the matched route. Take the following
237
- example:
236
+ the controller with a parameter from the matched route by its name . Take the
237
+ following example:
238
238
239
239
.. configuration-block ::
240
240
@@ -243,7 +243,7 @@ example:
243
243
# app/config/routing.yml
244
244
hello :
245
245
path : /hello/{firstName}/{lastName}
246
- defaults : { _controller: AcmeHelloBundle :Hello:index, color: green }
246
+ defaults : { _controller: AppBundle :Hello:index, color: green }
247
247
248
248
.. code-block :: xml
249
249
@@ -255,7 +255,7 @@ example:
255
255
http://symfony.com/schema/routing/routing-1.0.xsd" >
256
256
257
257
<route id =" hello" path =" /hello/{firstName}/{lastName}" >
258
- <default key =" _controller" >AcmeHelloBundle :Hello:index</default >
258
+ <default key =" _controller" >AppBundle :Hello:index</default >
259
259
<default key =" color" >green</default >
260
260
</route >
261
261
</routes >
@@ -268,7 +268,7 @@ example:
268
268
269
269
$collection = new RouteCollection();
270
270
$collection->add('hello', new Route('/hello/{firstName}/{lastName}', array(
271
- '_controller' => 'AcmeHelloBundle :Hello:index',
271
+ '_controller' => 'AppBundle :Hello:index',
272
272
'color' => 'green',
273
273
)));
274
274
@@ -377,8 +377,8 @@ you can take advantage of several helper methods.
377
377
Add the ``use `` statement atop the ``Controller `` class and then modify the
378
378
``HelloController `` to extend it::
379
379
380
- // src/Acme/HelloBundle /Controller/HelloController.php
381
- namespace Acme\HelloBundle \Controller;
380
+ // src/AppBundle /Controller/HelloController.php
381
+ namespace AppBundle \Controller;
382
382
383
383
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
384
384
use Symfony\Component\HttpFoundation\Response;
@@ -427,15 +427,17 @@ Common Controller Tasks
427
427
Though a controller can do virtually anything, most controllers will perform
428
428
the same basic tasks over and over again. These tasks, such as redirecting,
429
429
forwarding, rendering templates and accessing core services, are very easy
430
- to manage in Symfony.
430
+ to manage in Symfony when you're extending the base `` Controller `` class .
431
431
432
432
.. index ::
433
433
single: Controller; Redirecting
434
434
435
435
Redirecting
436
436
~~~~~~~~~~~
437
437
438
- If you want to redirect the user to another page, use the ``redirect() `` method::
438
+ If you want to redirect the user to another page, use the
439
+ :method: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ Controller::redirect `
440
+ method::
439
441
440
442
public function indexAction()
441
443
{
@@ -477,7 +479,7 @@ object that's returned from that controller::
477
479
478
480
public function indexAction($name)
479
481
{
480
- $response = $this->forward('AcmeHelloBundle:Hello :fancy', array(
482
+ $response = $this->forward('AppBundle:Something :fancy', array(
481
483
'name' => $name,
482
484
'color' => 'green',
483
485
));
@@ -489,22 +491,22 @@ object that's returned from that controller::
489
491
490
492
Notice that the ``forward() `` method uses the same string representation of
491
493
the controller used in the routing configuration. In this case, the target
492
- controller class will be ``HelloController `` inside some `` AcmeHelloBundle ``.
493
- The array passed to the method becomes the arguments on the resulting controller.
494
- This same interface is used when embedding controllers into templates (see
495
- :ref: `templating-embedding-controller `). The target controller method should
496
- look something like the following::
494
+ controller class will be ``SomethingController::fancyAction() `` inside the
495
+ `` AppBundle ``. The array passed to the method becomes the arguments on the
496
+ resulting controller. This same interface is used when embedding controllers
497
+ into templates (see :ref: `templating-embedding-controller `). The target
498
+ controller method should look something like the following::
497
499
498
500
public function fancyAction($name, $color)
499
501
{
500
502
// ... create and return a Response object
501
503
}
502
504
503
- And just like when creating a controller for a route, the order of the arguments
504
- to ``fancyAction `` doesn't matter. Symfony matches the index key names
505
- (e.g. ``name ``) with the method argument names (e.g. ``$name ``). If you
506
- change the order of the arguments, Symfony will still pass the correct
507
- value to each variable.
505
+ Just like when creating a controller for a route, the order of the arguments of
506
+ ``fancyAction `` doesn't matter. Symfony matches the index key names (e.g.
507
+ ``name ``) with the method argument names (e.g. ``$name ``). If you change the
508
+ order of the arguments, Symfony will still pass the correct value to each
509
+ variable.
508
510
509
511
.. tip ::
510
512
@@ -517,7 +519,7 @@ value to each variable.
517
519
use Symfony\Component\HttpKernel\HttpKernelInterface;
518
520
519
521
$path = array(
520
- '_controller' => 'AcmeHelloBundle:Hello :fancy',
522
+ '_controller' => 'AppBundle:Something :fancy',
521
523
'name' => $name,
522
524
'color' => 'green',
523
525
);
@@ -545,57 +547,45 @@ content from the template can be used to create a ``Response`` object::
545
547
546
548
use Symfony\Component\HttpFoundation\Response;
547
549
548
- $content = $this->renderView(
549
- 'AcmeHelloBundle:Hello:index.html.twig',
550
- array('name' => $name)
551
- );
550
+ $content = $this->renderView('Hello/index.html.twig', array('name' => $name));
552
551
553
552
return new Response($content);
554
553
555
554
This can even be done in just one step with the ``render() `` method, which
556
555
returns a ``Response `` object containing the content from the template::
557
556
558
- return $this->render(
559
- 'AcmeHelloBundle:Hello:index.html.twig',
560
- array('name' => $name)
561
- );
557
+ return $this->render('Hello/index.html.twig', array('name' => $name));
562
558
563
- In both cases, the ``Resources/views/Hello/index.html.twig `` template inside
564
- the `` AcmeHelloBundle `` will be rendered.
559
+ In both cases, the ``app/ Resources/views/Hello/index.html.twig `` template will
560
+ be rendered.
565
561
566
- The Symfony templating engine is explained in great detail in the
567
- :doc: `Templating </book/templating >` chapter.
562
+ .. sidebar :: Referencing Templates that Live inside the Bundle
568
563
569
- .. tip ::
564
+ You can also put templates in the ``Resources/views `` directory of a
565
+ bundle. You can then reference is with the
566
+ ``BundleName:DirectoryName:FileName `` syntax. E.g.
567
+ ``AppBundle:Hello:index.html.twig `` would refer to the template located in
568
+ ``src/AppBundle/Resources/views/Hello/index.html.twig ``.
570
569
571
- You can even avoid calling the ``render `` method by using the ``@Template ``
572
- annotation. See the
573
- :doc: `FrameworkExtraBundle documentation </bundles/SensioFrameworkExtraBundle/annotations/view >`
574
- more details.
570
+ The Symfony templating engine is explained in great detail in the
571
+ :doc: `Templating </book/templating >` chapter.
575
572
576
573
.. tip ::
577
574
578
575
The ``renderView `` method is a shortcut to direct use of the ``templating ``
579
576
service. The ``templating `` service can also be used directly::
580
577
581
578
$templating = $this->get('templating');
582
- $content = $templating->render(
583
- 'AcmeHelloBundle:Hello:index.html.twig',
584
- array('name' => $name)
585
- );
579
+ $content = $templating->render('Hello/index.html.twig', array('name' => $name));
586
580
587
581
.. note ::
588
582
589
583
It is possible to render templates in deeper subdirectories as well, however
590
584
be careful to avoid the pitfall of making your directory structure unduly
591
585
elaborate::
592
586
593
- $templating->render(
594
- 'AcmeHelloBundle:Hello/Greetings:index.html.twig',
595
- array('name' => $name)
596
- );
597
- // index.html.twig found in Resources/views/Hello/Greetings
598
- // is rendered.
587
+ $templating->render('Hello/Greetings/index.html.twig', array('name' => $name));
588
+ // renders app/Resources/views/Hello/Greetings/index.html.twig
599
589
600
590
.. index ::
601
591
single: Controller; Accessing services
@@ -751,7 +741,7 @@ the ``notice`` message:
751
741
<div class="flash-notice">
752
742
<?php echo "<div class='flash-error'>$message</div>" ?>
753
743
</div>
754
- <?php endforeach; ?>
744
+ <?php endforeach ?>
755
745
756
746
By design, flash messages are meant to live for exactly one request (they're
757
747
"gone in a flash"). They're designed to be used across redirects exactly as
0 commit comments