Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 218e2d4

Browse files
committed
WIP controller chapter
1 parent 8b923e6 commit 218e2d4

File tree

2 files changed

+27
-41
lines changed

2 files changed

+27
-41
lines changed

controller.rst

Lines changed: 25 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class::
6666
/**
6767
* @Route("/lucky/number/{max}")
6868
*/
69-
public function numberAction($max)
69+
public function number($max)
7070
{
7171
$number = mt_rand(0, $max);
7272

@@ -76,7 +76,7 @@ class::
7676
}
7777
}
7878

79-
The controller is the ``numberAction()`` method, which lives inside a
79+
The controller is the ``number()`` method, which lives inside a
8080
controller class ``LuckyController``.
8181

8282
This controller is pretty straightforward:
@@ -89,12 +89,10 @@ This controller is pretty straightforward:
8989
must return.
9090

9191
* *line 7*: The class can technically be called anything - but should end in the
92-
word ``Controller`` (this isn't *required*, but some shortcuts rely on this).
92+
word ``Controller``
9393

94-
* *line 12*: Each action method in a controller class is suffixed with ``Action``
95-
(again, this isn't *required*, but some shortcuts rely on this). This method
96-
is allowed to have a ``$max`` argument thanks to the ``{max}``
97-
:doc:`wildcard in the route </routing>`.
94+
* *line 12*: The action method is allowed to have a ``$max`` argument thanks to the
95+
``{max}`` :doc:`wildcard in the route </routing>`.
9896

9997
* *line 16*: The controller creates and returns a ``Response`` object.
10098

@@ -105,7 +103,8 @@ Mapping a URL to a Controller
105103
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
106104

107105
In order to *view* the result of this controller, you need to map a URL to it via
108-
a route. This was done above with the ``@Route("/lucky/number/{max}")`` annotation.
106+
a route. This was done above with the ``@Route("/lucky/number/{max}")``
107+
:ref:`route annotation <annotation-routes>`.
109108

110109
To see your page, go to this URL in your browser:
111110

@@ -121,12 +120,12 @@ For more information on routing, see :doc:`/routing`.
121120
The Base Controller Classes & Services
122121
--------------------------------------
123122

124-
For convenience, Symfony comes with two optional base
123+
To make life nicer, Symfony comes with two optional base
125124
:class:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller` and
126-
:class:`Symfony\\Bundle\\FrameworkBundle\\Controller\\AbstractController`
127-
classes. You can extend either to get access to a number of `helper methods`_.
125+
:class:`Symfony\\Bundle\\FrameworkBundle\\Controller\\AbstractController`.
126+
You can extend either to get access to some `helper methods`_.
128127

129-
Add the ``use`` statement atop the ``Controller`` class and then modify
128+
Add the ``use`` statement atop your controller class and then modify
130129
``LuckyController`` to extend it::
131130

132131
// src/Controller/LuckyController.php
@@ -146,11 +145,12 @@ and many others that you'll learn about next.
146145

147146
.. tip::
148147

149-
You can extend either ``Controller`` or ``AbstractController``. The difference
150-
is that when you extend ``AbstractController``, you can't access services directly
151-
via ``$this->get()`` or ``$this->container->get()``. This forces you to write
152-
more robust code to access services. But if you *do* need direct access to the
153-
container, using ``Controller`` is fine.
148+
What's the difference between ``Controller`` or ``AbstractController``. Not much:
149+
both are identical, except that ``AbstractController`` is more restrictive: it
150+
does not allow you to access services directly via ``$this->get()`` or
151+
``$this->container->get()``. This forces you to write more robust code to access
152+
services. But if you *do* need direct access to the container, using ``Controller``
153+
is fine.
154154

155155
.. index::
156156
single: Controller; Redirecting
@@ -169,11 +169,17 @@ Redirecting
169169
If you want to redirect the user to another page, use the ``redirectToRoute()``
170170
and ``redirect()`` methods::
171171

172+
use Symfony\Component\HttpFoundation\RedirectResponse;
173+
174+
// ...
172175
public function indexAction()
173176
{
174177
// redirect to the "homepage" route
175178
return $this->redirectToRoute('homepage');
176179

180+
// redirectToRoute is a shortcut for:
181+
// return new RedirectResponse($this->generateUrl('homepage'));
182+
177183
// do a permanent - 301 redirect
178184
return $this->redirectToRoute('homepage', array(), 301);
179185

@@ -189,23 +195,9 @@ For more information, see the :doc:`Routing article </routing>`.
189195
.. caution::
190196

191197
The ``redirect()`` method does not check its destination in any way. If you
192-
redirect to some URL provided by the end-users, your application may be open
198+
redirect to some URL provided byend-users, your application may be open
193199
to the `unvalidated redirects security vulnerability`_.
194200

195-
196-
.. tip::
197-
198-
The ``redirectToRoute()`` method is simply a shortcut that creates a
199-
``Response`` object that specializes in redirecting the user. It's
200-
equivalent to::
201-
202-
use Symfony\Component\HttpFoundation\RedirectResponse;
203-
204-
public function indexAction()
205-
{
206-
return new RedirectResponse($this->generateUrl('homepage'));
207-
}
208-
209201
.. index::
210202
single: Controller; Rendering templates
211203

@@ -221,15 +213,7 @@ object for you::
221213
// renders templates/lucky/number.html.twig
222214
return $this->render('lucky/number.html.twig', array('name' => $name));
223215

224-
Templates can also live in deeper sub-directories. Just try to avoid
225-
creating unnecessarily deep structures::
226-
227-
// renders templates/lottery/lucky/number.html.twig
228-
return $this->render('lottery/lucky/number.html.twig', array(
229-
'name' => $name,
230-
));
231-
232-
The Symfony templating system and Twig are explained more in the
216+
Templating and Twig are explained more in the
233217
:doc:`Creating and Using Templates article </templating>`.
234218

235219
.. index::

page_creation.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ to creating a page?
8989
return a ``Response`` object. You'll learn more about :doc:`controllers </controller>`
9090
in their own section, including how to return JSON responses.
9191

92+
.. _annotation-routes:
93+
9294
Annotation Routes
9395
-----------------
9496

0 commit comments

Comments
 (0)