Skip to content

Commit 2c190ed

Browse files
committed
feature #4427 Update most important book articles to follow the best practices (WouterJ)
This PR was merged into the 2.3 branch. Discussion ---------- Update most important book articles to follow the best practices | Q | A | --- | --- | Doc fix? | yes | New docs? | no | Applies to | all (or 2.6?) | Fixed ticket | lots of complaints :) I've decided to do only the most important articles for the first PR, otherwise the diff will be too big to get a nice review. I'll do a seperate PR for the Page Creation article, since that needed quite so rewriting. Btw, I also couldn't resist to fix other minor things :) Commits ------- e56c272 Fixed error 9678b61 Proofread templating 51773f4 Proofread routing article 91c8981 Used annotations for routing 210bb4b Some minor things 4da1bcf Use AppBundle 10b3c7c Other random fixes a8a75d7 Changed Bundle:Controller:Template to bundle:dir:file db3086d Moved templates to app and controllers to AppBundle ab2e2c7 Other fixes 18e3a31 Moved templates to app c8f5ad5 Updated to use AppBundle b735642 Fixed some other minor stuff 0ef1c68 Moved templates to app/Resources/views 87b324a Renamed AcmeHelloBundle to AppBundle
2 parents ddd4a3b + e56c272 commit 2c190ed

File tree

5 files changed

+543
-369
lines changed

5 files changed

+543
-369
lines changed

best_practices/templates.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ In addition, Twig is the only template format with guaranteed support in Symfony
2222
3.0. As a matter of fact, PHP may be removed from the officially supported
2323
template engines.
2424

25+
.. _best_practices-template-location:
26+
2527
Template Locations
2628
------------------
2729

book/controller.rst

Lines changed: 53 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,9 @@ or a ``Closure``), in Symfony, a controller is usually a single method inside
9191
a controller object. Controllers are also called *actions*.
9292

9393
.. code-block:: php
94-
:linenos:
9594
96-
// src/Acme/HelloBundle/Controller/HelloController.php
97-
namespace Acme\HelloBundle\Controller;
95+
// src/AppBundle/Controller/HelloController.php
96+
namespace AppBundle\Controller;
9897
9998
use Symfony\Component\HttpFoundation\Response;
10099
@@ -151,7 +150,7 @@ to the controller:
151150
# app/config/routing.yml
152151
hello:
153152
path: /hello/{name}
154-
defaults: { _controller: AcmeHelloBundle:Hello:index }
153+
defaults: { _controller: AppBundle:Hello:index }
155154
156155
.. code-block:: xml
157156
@@ -163,7 +162,7 @@ to the controller:
163162
http://symfony.com/schema/routing/routing-1.0.xsd">
164163
165164
<route id="hello" path="/hello/{name}">
166-
<default key="_controller">AcmeHelloBundle:Hello:index</default>
165+
<default key="_controller">AppBundle:Hello:index</default>
167166
</route>
168167
</routes>
169168
@@ -175,7 +174,7 @@ to the controller:
175174
176175
$collection = new RouteCollection();
177176
$collection->add('hello', new Route('/hello/{name}', array(
178-
'_controller' => 'AcmeHelloBundle:Hello:index',
177+
'_controller' => 'AppBundle:Hello:index',
179178
)));
180179
181180
return $collection;
@@ -184,10 +183,10 @@ Going to ``/hello/ryan`` now executes the ``HelloController::indexAction()``
184183
controller and passes in ``ryan`` for the ``$name`` variable. Creating a
185184
"page" means simply creating a controller method and associated route.
186185

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``.
188187
Symfony uses a flexible string notation to refer to different controllers.
189188
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
191190
method ``indexAction()`` is then executed.
192191

193192
For more details on the string format used to reference different controllers,
@@ -202,7 +201,8 @@ see :ref:`controller-string-syntax`.
202201

203202
.. tip::
204203

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>`.
206206

207207
.. index::
208208
single: Controller; Controller arguments
@@ -212,29 +212,29 @@ see :ref:`controller-string-syntax`.
212212
Route Parameters as Controller Arguments
213213
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
214214

215-
You already know that the ``_controller`` parameter ``AcmeHelloBundle:Hello:index``
215+
You already know that the ``_controller`` parameter ``AppBundle:Hello:index``
216216
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::
219219

220-
// src/Acme/HelloBundle/Controller/HelloController.php
221-
namespace Acme\HelloBundle\Controller;
220+
// src/AppBundle/Controller/HelloController.php
221+
namespace AppBundle\Controller;
222222

223223
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
224224

225225
class HelloController extends Controller
226226
{
227227
public function indexAction($name)
228228
{
229-
// ...
229+
// ...
230230
}
231231
}
232232

233233
The controller has a single argument, ``$name``, which corresponds to the
234234
``{name}`` parameter from the matched route (``ryan`` in the example). In
235235
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:
238238

239239
.. configuration-block::
240240

@@ -243,7 +243,7 @@ example:
243243
# app/config/routing.yml
244244
hello:
245245
path: /hello/{firstName}/{lastName}
246-
defaults: { _controller: AcmeHelloBundle:Hello:index, color: green }
246+
defaults: { _controller: AppBundle:Hello:index, color: green }
247247
248248
.. code-block:: xml
249249
@@ -255,7 +255,7 @@ example:
255255
http://symfony.com/schema/routing/routing-1.0.xsd">
256256
257257
<route id="hello" path="/hello/{firstName}/{lastName}">
258-
<default key="_controller">AcmeHelloBundle:Hello:index</default>
258+
<default key="_controller">AppBundle:Hello:index</default>
259259
<default key="color">green</default>
260260
</route>
261261
</routes>
@@ -268,7 +268,7 @@ example:
268268
269269
$collection = new RouteCollection();
270270
$collection->add('hello', new Route('/hello/{firstName}/{lastName}', array(
271-
'_controller' => 'AcmeHelloBundle:Hello:index',
271+
'_controller' => 'AppBundle:Hello:index',
272272
'color' => 'green',
273273
)));
274274
@@ -377,8 +377,8 @@ you can take advantage of several helper methods.
377377
Add the ``use`` statement atop the ``Controller`` class and then modify the
378378
``HelloController`` to extend it::
379379

380-
// src/Acme/HelloBundle/Controller/HelloController.php
381-
namespace Acme\HelloBundle\Controller;
380+
// src/AppBundle/Controller/HelloController.php
381+
namespace AppBundle\Controller;
382382

383383
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
384384
use Symfony\Component\HttpFoundation\Response;
@@ -422,15 +422,17 @@ Common Controller Tasks
422422
Though a controller can do virtually anything, most controllers will perform
423423
the same basic tasks over and over again. These tasks, such as redirecting,
424424
forwarding, rendering templates and accessing core services, are very easy
425-
to manage in Symfony.
425+
to manage in Symfony when you're extending the base ``Controller`` class.
426426

427427
.. index::
428428
single: Controller; Redirecting
429429

430430
Redirecting
431431
~~~~~~~~~~~
432432

433-
If you want to redirect the user to another page, use the ``redirect()`` method::
433+
If you want to redirect the user to another page, use the
434+
:method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::redirect`
435+
method::
434436

435437
public function indexAction()
436438
{
@@ -472,7 +474,7 @@ object that's returned from that controller::
472474

473475
public function indexAction($name)
474476
{
475-
$response = $this->forward('AcmeHelloBundle:Hello:fancy', array(
477+
$response = $this->forward('AppBundle:Something:fancy', array(
476478
'name' => $name,
477479
'color' => 'green',
478480
));
@@ -484,22 +486,22 @@ object that's returned from that controller::
484486

485487
Notice that the ``forward()`` method uses the same string representation of
486488
the controller used in the routing configuration. In this case, the target
487-
controller class will be ``HelloController`` inside some ``AcmeHelloBundle``.
488-
The array passed to the method becomes the arguments on the resulting controller.
489-
This same interface is used when embedding controllers into templates (see
490-
:ref:`templating-embedding-controller`). The target controller method should
491-
look something like the following::
489+
controller class will be ``SomethingController::fancyAction()`` inside the
490+
``AppBundle``. The array passed to the method becomes the arguments on the
491+
resulting controller. This same interface is used when embedding controllers
492+
into templates (see :ref:`templating-embedding-controller`). The target
493+
controller method should look something like the following::
492494

493495
public function fancyAction($name, $color)
494496
{
495497
// ... create and return a Response object
496498
}
497499

498-
And just like when creating a controller for a route, the order of the arguments
499-
to ``fancyAction`` doesn't matter. Symfony matches the index key names
500-
(e.g. ``name``) with the method argument names (e.g. ``$name``). If you
501-
change the order of the arguments, Symfony will still pass the correct
502-
value to each variable.
500+
Just like when creating a controller for a route, the order of the arguments of
501+
``fancyAction`` doesn't matter. Symfony matches the index key names (e.g.
502+
``name``) with the method argument names (e.g. ``$name``). If you change the
503+
order of the arguments, Symfony will still pass the correct value to each
504+
variable.
503505

504506
.. tip::
505507

@@ -512,7 +514,7 @@ value to each variable.
512514
use Symfony\Component\HttpKernel\HttpKernelInterface;
513515

514516
$path = array(
515-
'_controller' => 'AcmeHelloBundle:Hello:fancy',
517+
'_controller' => 'AppBundle:Something:fancy',
516518
'name' => $name,
517519
'color' => 'green',
518520
);
@@ -540,57 +542,45 @@ content from the template can be used to create a ``Response`` object::
540542

541543
use Symfony\Component\HttpFoundation\Response;
542544

543-
$content = $this->renderView(
544-
'AcmeHelloBundle:Hello:index.html.twig',
545-
array('name' => $name)
546-
);
545+
$content = $this->renderView('Hello/index.html.twig', array('name' => $name));
547546

548547
return new Response($content);
549548

550549
This can even be done in just one step with the ``render()`` method, which
551550
returns a ``Response`` object containing the content from the template::
552551

553-
return $this->render(
554-
'AcmeHelloBundle:Hello:index.html.twig',
555-
array('name' => $name)
556-
);
552+
return $this->render('Hello/index.html.twig', array('name' => $name));
557553

558-
In both cases, the ``Resources/views/Hello/index.html.twig`` template inside
559-
the ``AcmeHelloBundle`` will be rendered.
554+
In both cases, the ``app/Resources/views/Hello/index.html.twig`` template will
555+
be rendered.
560556

561-
The Symfony templating engine is explained in great detail in the
562-
:doc:`Templating </book/templating>` chapter.
557+
.. sidebar:: Referencing Templates that Live inside the Bundle
563558

564-
.. tip::
559+
You can also put templates in the ``Resources/views`` directory of a
560+
bundle. You can then reference is with the
561+
``BundleName:DirectoryName:FileName`` syntax. E.g.
562+
``AppBundle:Hello:index.html.twig`` would refer to the template located in
563+
``src/AppBundle/Resources/views/Hello/index.html.twig``.
565564

566-
You can even avoid calling the ``render`` method by using the ``@Template``
567-
annotation. See the
568-
:doc:`FrameworkExtraBundle documentation </bundles/SensioFrameworkExtraBundle/annotations/view>`
569-
more details.
565+
The Symfony templating engine is explained in great detail in the
566+
:doc:`Templating </book/templating>` chapter.
570567

571568
.. tip::
572569

573570
The ``renderView`` method is a shortcut to direct use of the ``templating``
574571
service. The ``templating`` service can also be used directly::
575572

576573
$templating = $this->get('templating');
577-
$content = $templating->render(
578-
'AcmeHelloBundle:Hello:index.html.twig',
579-
array('name' => $name)
580-
);
574+
$content = $templating->render('Hello/index.html.twig', array('name' => $name));
581575

582576
.. note::
583577

584578
It is possible to render templates in deeper subdirectories as well, however
585579
be careful to avoid the pitfall of making your directory structure unduly
586580
elaborate::
587581

588-
$templating->render(
589-
'AcmeHelloBundle:Hello/Greetings:index.html.twig',
590-
array('name' => $name)
591-
);
592-
// index.html.twig found in Resources/views/Hello/Greetings
593-
// is rendered.
582+
$templating->render('Hello/Greetings/index.html.twig', array('name' => $name));
583+
// renders app/Resources/views/Hello/Greetings/index.html.twig
594584

595585
.. index::
596586
single: Controller; Accessing services

book/from_flat_php_to_symfony2.rst

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -547,8 +547,8 @@ from scratch, you could at least use Symfony's standalone `Routing`_ and
547547
Instead of re-solving common problems, you can let Symfony take care of
548548
them for you. Here's the same sample application, now built in Symfony::
549549

550-
// src/Acme/BlogBundle/Controller/BlogController.php
551-
namespace Acme\BlogBundle\Controller;
550+
// src/AppBundle/Controller/BlogController.php
551+
namespace AppBundle\Controller;
552552

553553
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
554554

@@ -561,40 +561,34 @@ them for you. Here's the same sample application, now built in Symfony::
561561
->createQuery('SELECT p FROM AcmeBlogBundle:Post p')
562562
->execute();
563563

564-
return $this->render(
565-
'AcmeBlogBundle:Blog:list.html.php',
566-
array('posts' => $posts)
567-
);
564+
return $this->render('Blog/list.html.php', array('posts' => $posts));
568565
}
569566

570567
public function showAction($id)
571568
{
572569
$post = $this->get('doctrine')
573570
->getManager()
574-
->getRepository('AcmeBlogBundle:Post')
571+
->getRepository('AppBundle:Post')
575572
->find($id);
576573

577574
if (!$post) {
578575
// cause the 404 page not found to be displayed
579576
throw $this->createNotFoundException();
580577
}
581578

582-
return $this->render(
583-
'AcmeBlogBundle:Blog:show.html.php',
584-
array('post' => $post)
585-
);
579+
return $this->render('Blog/show.html.php', array('post' => $post));
586580
}
587581
}
588582

589-
The two controllers are still lightweight. Each uses the :doc:`Doctrine ORM library </book/doctrine>`
590-
to retrieve objects from the database and the Templating component to
591-
render a template and return a ``Response`` object. The list template is
592-
now quite a bit simpler:
583+
The two controllers are still lightweight. Each uses the
584+
:doc:`Doctrine ORM library </book/doctrine>` to retrieve objects from the
585+
database and the Templating component to render a template and return a
586+
``Response`` object. The list template is now quite a bit simpler:
593587

594588
.. code-block:: html+php
595589

596-
<!-- src/Acme/BlogBundle/Resources/views/Blog/list.html.php -->
597-
<?php $view->extend('::layout.html.php') ?>
590+
<!-- app/Resources/views/Blog/list.html.php -->
591+
<?php $view->extend('layout.html.php') ?>
598592

599593
<?php $view['slots']->set('title', 'List of Posts') ?>
600594

@@ -644,11 +638,11 @@ A routing configuration map provides this information in a readable format:
644638
# app/config/routing.yml
645639
blog_list:
646640
path: /blog
647-
defaults: { _controller: AcmeBlogBundle:Blog:list }
641+
defaults: { _controller: AppBundle:Blog:list }
648642
649643
blog_show:
650644
path: /blog/show/{id}
651-
defaults: { _controller: AcmeBlogBundle:Blog:show }
645+
defaults: { _controller: AppBundle:Blog:show }
652646
653647
Now that Symfony is handling all the mundane tasks, the front controller
654648
is dead simple. And since it does so little, you'll never have to touch
@@ -716,8 +710,8 @@ for example, the list template written in Twig:
716710

717711
.. code-block:: html+jinja
718712

719-
{# src/Acme/BlogBundle/Resources/views/Blog/list.html.twig #}
720-
{% extends "::layout.html.twig" %}
713+
{# app/Resources/views/Blog/list.html.twig #}
714+
{% extends "layout.html.twig" %}
721715

722716
{% block title %}List of Posts{% endblock %}
723717

0 commit comments

Comments
 (0)