Skip to content

Commit 6214eb6

Browse files
committed
Making minor tweaks after reading through the changes in those PR's
1 parent 693e1e1 commit 6214eb6

File tree

2 files changed

+68
-106
lines changed

2 files changed

+68
-106
lines changed

book/controller.rst

Lines changed: 68 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,11 @@ Every request handled by a Symfony project goes through the same simple lifecycl
5454
The framework takes care of all the repetitive stuff: you just need to write
5555
your custom code in the controller function:
5656

57-
#. Each request executes a single front controller file (e.g. on production
58-
``app.php`` or on development ``app_dev.php``) that bootstraps the
59-
application;
57+
#. Each request executes a single front controller file (e.g. ``app.php`` on production
58+
or ``app_dev.php`` on development) that bootstraps the application;
6059

61-
#. Front controller's only job is to initialize Symfony's engine (called the
62-
`Kernel`) and pass it a ``Request`` object to handle;
60+
#. The front controller's only job is to initialize Symfony's engine (called the
61+
``Kernel``) and pass it a ``Request`` object to handle;
6362

6463
#. The Symfony core asks the router to inspect the request;
6564

@@ -80,18 +79,20 @@ that maps a URL to that controller (#4).
8079
.. image:: /images/http-xkcd-request.png
8180
:align: center
8281

83-
Though similarly named, a "front controller" is different from the PHP
84-
functions called "controllers" talked about in this chapter. A front
85-
controller is a short PHP file that lives in your ``web/`` directory
86-
through which all requests are directed. A typical application will
87-
have a production front controller (e.g. ``app.php``) and a development
88-
front controller (e.g. ``app_dev.php``). You'll likely never need to
89-
edit, view or worry about the front controllers in your application.
90-
The "controller class" is a convenient way to group several "controllers",
91-
also called actions, together in one class (e.g. ``updateAction()``,
92-
``deleteAction()``, etc). So, a controller is a method inside a controller
93-
class. They hold your code which creates and returns the appropriate
94-
``Response`` object.
82+
.. note::
83+
84+
Though similarly named, a "front controller" is different from the PHP
85+
functions called "controllers" talked about in this chapter. A front
86+
controller is a short PHP file that lives in your ``web/`` directory
87+
through which all requests are directed. A typical application will
88+
have a production front controller (e.g. ``app.php``) and a development
89+
front controller (e.g. ``app_dev.php``). You'll likely never need to
90+
edit, view or worry about the front controllers in your application.
91+
The "controller class" is a convenient way to group several "controllers",
92+
also called actions, together in one class (e.g. ``updateAction()``,
93+
``deleteAction()``, etc). So, a controller is a method inside a controller
94+
class. They hold your code which creates and returns the appropriate
95+
``Response`` object.
9596

9697
.. index::
9798
single: Controller; Simple example
@@ -150,7 +151,7 @@ Mapping a URL to a Controller
150151

151152
The new controller returns a simple HTML page. To actually view this page
152153
in your browser, you need to create a route, which maps a specific URL path
153-
to the controller::
154+
to the controller:
154155

155156
.. configuration-block::
156157

@@ -218,17 +219,14 @@ simply creating a controller method and an associated route.
218219

219220
Simple, right?
220221

221-
.. sidebar:: Logical controller name
222+
.. sidebar:: The AppBundle:Hello:index controller syntax
222223

223224
If you use the YAML or XML formats, you'll refer to the controller
224-
using a special shortcut syntax called *logical controller name*
225+
using a special shortcut syntax called the *logical controller name*
225226
which, for example, looks like ``AppBundle:Hello:index``. For more
226227
details on the controller format, read
227228
:ref:`controller-string-syntax` subtitle of the Routing chapter.
228229

229-
You can learn much more about the routing system in the
230-
:doc:`Routing chapter </book/routing>`.
231-
232230
.. index::
233231
single: Controller; Controller arguments
234232

@@ -255,13 +253,13 @@ method::
255253
}
256254

257255
The controller has a single argument, ``$name``, which corresponds to the
258-
``{name}`` placehold from the matched route (``ryan`` if you go to
259-
``/hello/ryan``). When executing controller, Symfony matches each argument
260-
with a placehold from the route. So the value for ``{name}`` is passed
256+
``{name}`` placeholder from the matched route (e.g. ``ryan`` if you go to
257+
``/hello/ryan``). When executing the controller, Symfony matches each argument
258+
with a placeholder from the route. So the value for ``{name}`` is passed
261259
to ``$name``. Just make sure they the name of the placeholder is the
262260
same as the name of the argument variable.
263261

264-
Take the following more-interesting example, where controller has two
262+
Take the following more-interesting example, where the controller has two
265263
arguments::
266264

267265
.. configuration-block::
@@ -362,11 +360,6 @@ Keep the following guidelines in mind while you develop.
362360

363361
.. tip::
364362

365-
Every route also has a special ``_route`` parameter (you will learn more
366-
about this in the :ref:`Routing chapter <book-special-routing-parameters>`),
367-
which is equal to the name of the route that was matched (e.g. ``hello``).
368-
Though not usually useful, this is also available as a controller argument.
369-
370363
You can also pass other variables from your route to your controller
371364
arguments. See :doc:`/cookbook/routing/extra_information`.
372365

@@ -379,7 +372,7 @@ The Base Controller Class
379372

380373
For convenience, Symfony comes with an optional base
381374
:class:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller` class.
382-
If you extend it, this wont change anything about how your controller
375+
If you extend it, this won't change anything about how your controller
383376
works, but you'll get access to a number of **helper methods** and the
384377
**service container** (see :ref:`controller-accessing-services`): an
385378
array-like object that gives you access to every useful object in the
@@ -388,7 +381,7 @@ with a service object that can render Twig templates, another that can
388381
log messages and many more.
389382

390383
Add the ``use`` statement atop the ``Controller`` class and then modify
391-
the ``HelloController`` to extend it::
384+
``HelloController`` to extend it::
392385

393386
// src/AppBundle/Controller/HelloController.php
394387
namespace AppBundle\Controller;
@@ -428,10 +421,10 @@ method is just a helper method that generates the URL for a given route.
428421
Redirecting
429422
~~~~~~~~~~~
430423

431-
To redirect the user's browser to another page **internally**, use the ``generateUrl()``
424+
To redirect the user's browser to another page of your app, use the ``generateUrl()``
432425
method in combination with another helper method called
433426
:method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::redirect`
434-
which needs URL as an argument::
427+
which takes a URL as an argument::
435428

436429
public function indexAction()
437430
{
@@ -446,7 +439,7 @@ perform a 301 (permanent) redirect, modify the second argument::
446439
return $this->redirect($this->generateUrl('homepage'), 301);
447440
}
448441

449-
To redirect **externally**, use ``redirect()`` and pass it the external URL::
442+
To redirect to an *external* site, use ``redirect()`` and pass it the external URL::
450443

451444
public function indexAction()
452445
{
@@ -472,15 +465,14 @@ For more information, see the :doc:`Routing chapter </book/routing>`.
472465
Rendering Templates
473466
~~~~~~~~~~~~~~~~~~~
474467

475-
To serve HTML, template needs to be rendered and the content put into
476-
the ``Response`` object. The shortcut method
477-
:method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::render`
478-
of ``Controller`` class does just that::
468+
If you're serving HTML, you'll want to render a template. The ``render()``
469+
method renders a template **and** puts that content into a ``Response``
470+
object for you::
479471

480472
// renders app/Resources/views/hello/index.html.twig
481473
return $this->render('hello/index.html.twig', array('name' => $name));
482474

483-
Templates can be also put in deeper sub-directories. Just try to avoid
475+
Templates can also live in deeper sub-directories. Just try to avoid
484476
creating unnecessarily deep structures::
485477

486478
// renders app/Resources/views/hello/greetings/index.html.twig
@@ -491,29 +483,18 @@ creating unnecessarily deep structures::
491483
Templates are a generic way to render content in *any* format. And while in
492484
most cases you'll use templates to render HTML content, a template can just
493485
as easily generate JavaScript, CSS, XML or any other format you can dream of.
494-
To learn how to render different templating formats read :ref:`template-formats`
486+
To learn how to render different templating formats read the :ref:`template-formats`
495487
section of the Creating and Using Templates chapter.
496488

497489
The Symfony templating engine is explained in great detail in the
498490
:doc:`Creating and Using Templates chapter </book/templating>`.
499491

500492
.. sidebar:: Templating Naming Pattern
501493

502-
Templates for a bundle can be put in the ``src/path/to/bundle/Resources/views``
503-
directory of a bundle and reference with a special shortcut syntax called
504-
*logical name* which, for example, looks like ``AppBundle:Hello:index.html.twig`` or
505-
``AppBundle::layout.html.twig``. The pattern has three parts, each separated
506-
by a colon. Syntax used to specify a template for a specific page::
507-
508-
**bundle**:**directory**:**filename**
509-
510-
Syntax used to refer to a base template that's specific to the bundle::
511-
512-
``**bundle**::**filename**``
513-
514-
For more details on the template format, read
515-
:ref:`template-referencing-in-bundle` subtitle of the Creating and
516-
Using Templates chapter.
494+
You can also put templates in the ``Resources/views`` directory of a bundle and
495+
reference them with a special shortcut syntax like ``@AppBundle/Hello/index.html.twig``
496+
or ``@AppBundle/layout.html.twig``. These would live in at ``Resources/views/Hello/index.html.twig``
497+
and ``Resources/views/layout.html.twig`` inside the bundle respectively.
517498

518499
.. index::
519500
single: Controller; Accessing services
@@ -540,7 +521,7 @@ need::
540521
$mailer = $this->get('mailer');
541522

542523
What other services exist? To list all services, use the ``container:debug``
543-
console command::
524+
console command:
544525

545526
.. code-block:: bash
546527
@@ -583,11 +564,11 @@ Symfony will automatically return a 500 HTTP response code.
583564
throw new \Exception('Something went wrong!');
584565
585566
In every case, an error page is shown to the end user and a full debug
586-
error page is shown to the developer (i.e. when you're using ``app_dev.php``
567+
error page is shown to the developer (i.e. when you're using the ``app_dev.php``
587568
front controller - see :ref:`page-creation-environments`).
588569

589570
You'll want to customize the error page your user sees. To do that, see
590-
the cookbook article ":doc:`/cookbook/controller/error_pages`" cookbook recipe.
571+
the ":doc:`/cookbook/controller/error_pages`" cookbook recipe.
591572

592573
.. index::
593574
single: Controller; The session
@@ -620,11 +601,11 @@ about the user (be it a real person using a browser, a bot, or a web service)
620601
between requests. By default, Symfony stores the attributes in a cookie
621602
by using the native PHP sessions.
622603

623-
To retrieving the session we have to call the
604+
To retrieve the session, call
624605
:method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::getSession`
625-
method on the ``Request`` object inside a controller. Method returns a
626-
:class:`Symfony\\Component\\HttpFoundation\\Session\\SessionInterface`
627-
with all the methods to handle a session::
606+
method on the ``Request`` object. This method returns a
607+
:class:`Symfony\\Component\\HttpFoundation\\Session\\SessionInterface` with easy
608+
methods for storing and fetching things from the session::
628609

629610
use Symfony\Component\HttpFoundation\Request;
630611

@@ -722,10 +703,9 @@ read any flash messages from the session:
722703
The Request and Response Object
723704
-------------------------------
724705

725-
As already mentioned a :ref:`little earlier <book-controller-request-argument>`,
726-
besides the values of the routing parameters, the controller has also access
727-
to the ``Request`` object. The framework injects the ``Request`` object
728-
in the controller if a variable is type-hinted with ``Request`` class::
706+
As mentioned :ref:`earlier <book-controller-request-argument>`, the framework will
707+
pass the ``Request`` object to any controller argument taht is type-hinted with
708+
the ``Request`` class::
729709

730710
use Symfony\Component\HttpFoundation\Request;
731711

@@ -753,14 +733,14 @@ in the controller if a variable is type-hinted with ``Request`` class::
753733
$request->headers->get('content_type');
754734
}
755735

756-
``Request`` class has several public properties via which information about the client
757-
request can be accessed.
736+
The ``Request`` class has several public properties and methods that return any
737+
information you need about the request.
758738

759-
Like the ``Request``, the ``Response`` object has also a public ``headers`` property
760-
which is a :class:`Symfony\\Component\\HttpFoundation\\ResponseHeaderBag` instance.
761-
``ResponseHeaderBag`` instances have methods for getting and setting the response
762-
headers. The header names are normalized so that using ``Content-Type`` is equivalent
763-
to ``content-type`` or even ``content_type``.
739+
Like the ``Request``, the ``Response`` object has also a public ``headers`` property.
740+
This is a :class:`Symfony\\Component\\HttpFoundation\\ResponseHeaderBag` that has
741+
some nice methods for getting and setting response headers. The header names are
742+
normalized so that using ``Content-Type`` is equivalent to ``content-type`` or even
743+
``content_type``.
764744

765745
The only requirement for a controller is to return a ``Response`` object.
766746
The :class:`Symfony\\Component\\HttpFoundation\\Response` class is an
@@ -807,15 +787,11 @@ and template are needed). See cookbook article
807787
Forwarding to Another Controller
808788
--------------------------------
809789

810-
We already saw how to redirect the :ref:`user's browser <book-redirecting-users-browser>`
811-
to another page internally or to some external URL.
812-
813790
Though not very common, you can also forward to another controller
814-
internally with the basic ``Controller`` class method
815-
:method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::forward`.
816-
Instead of redirecting the user's browser, method makes an internal
817-
sub-request, and calls the defined controller. The ``forward()`` method returns
818-
the ``Response`` object that's returned from *that* controller::
791+
internally with the :method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::forward`
792+
method. Instead of redirecting the user's browser, this makes an "internal" sub-request
793+
and calls the defined controller. The ``forward()`` method returns the ``Response``
794+
object that's returned from *that* controller::
819795

820796
public function indexAction($name)
821797
{
@@ -829,38 +805,25 @@ the ``Response`` object that's returned from *that* controller::
829805
return $response;
830806
}
831807

832-
The array passed to the method becomes the arguments on the resulting controller.
833-
The target controller method would look something like this::
808+
The array passed to the method becomes the arguments for the resulting controller.
809+
The target controller method might look something like this::
834810

835811
public function fancyAction($name, $color)
836812
{
837813
// ... create and return a Response object
838814
}
839815

840-
.. sidebar:: Logical controller name
841-
842-
Notice that the ``forward()`` method uses a special string representation
843-
called *logical controller name* which, for example, looks like
844-
``AppBundle:Hello:index``. For more details on the controller format, read
845-
:ref:`controller-string-syntax` subtitle of the Routing chapter.
846-
847-
You can learn much more about the routing system in the
848-
:doc:`Routing chapter </book/routing>`.
849-
850-
Just like when creating a controller for a route, the order of the
851-
arguments of ``fancyAction()`` doesn't matter. Symfony matches the route
852-
placeholder names (e.g. ``{name}``) with the method argument names (e.g. ``$name``).
853-
If you change the order of the arguments, Symfony will still pass the correct
854-
value to each variable.
816+
Just like when creating a controller for a route, the order of the arguments of
817+
``fancyAction()`` doesn't matter: the matching is done by name.
855818

856819
Checking the Validity of a CSRF Token inside Controller
857820
-------------------------------------------------------
858821

859-
Sometimes you want to use :ref:`CSRF protection <forms-csrf>` in a controller where
860-
you don't want to use a Symfony form.
822+
You may sometimes want to use :ref:`CSRF protection <forms-csrf>` in a controller where
823+
you don't have a Symfony form.
861824

862825
If, for example, you're doing a DELETE action, you can use the
863-
:method:`Symfony\\Component\\Form\\Extension\\Csrf\\CsrfProvider\\SessionCsrfProvider::isCsrfTokenValid`
826+
:method:`Symfony\\Component\\Form\\Extension\\Csrf\\CsrfProvider\\CsrfProviderInterface::isCsrfTokenValid`
864827
method to check the CSRF token::
865828

866829
$csrf = $this->container->get('form.csrf_provider');
@@ -894,4 +857,5 @@ Learn more from the Cookbook
894857

895858
* :doc:`/cookbook/controller/error_pages`
896859
* :doc:`/cookbook/controller/service`
860+
897861
.. _`Controller class`: https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php

book/routing.rst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,8 +1050,6 @@ a slash. URLs matching this route might look like:
10501050
Symfony provides you with a way to do this by leveraging service container
10511051
parameters. Read more about this in ":doc:`/cookbook/routing/service_container_parameters`".
10521052

1053-
.. _book-special-routing-parameters:
1054-
10551053
Special Routing Parameters
10561054
~~~~~~~~~~~~~~~~~~~~~~~~~~
10571055

0 commit comments

Comments
 (0)