Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ceb7b94

Browse filesBrowse files
committed
Big update based on feedback from xabbuh and WouterJ
1 parent 6ef10db commit ceb7b94
Copy full SHA for ceb7b94

File tree

Expand file treeCollapse file tree

1 file changed

+23
-47
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+23
-47
lines changed

‎book/controller.rst

Copy file name to clipboardExpand all lines: book/controller.rst
+23-47Lines changed: 23 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -202,18 +202,18 @@ to the controller:
202202
return $collection;
203203
204204
Now, you can go to ``/hello/ryan`` (e.g. ``http://localhost:8000/app_dev.php/hello/ryan``
205-
if you're using the :doc:`built-in web server </cookbook/webserver/built_in>`_)
205+
if you're using the :doc:`built-in web server </cookbook/webserver/built_in>`)
206206
and Symfony will execute the ``HelloController::indexAction()`` controller
207207
and pass in ``ryan`` for the ``$name`` variable. Creating a "page" means
208-
simply creating a controller method and associated route.
208+
simply creating a controller method and an associated route.
209209

210210
Simple, right?
211211

212-
.. sidebar:: The AppBundle:Hello:index syntax for YML and XML
212+
.. sidebar:: The AppBundle:Hello:index controller syntax
213213

214214
If you use the YML or XML formats, you'll refer to the controller using
215215
a special shortcut syntax: ``AppBundle:Hello:index``. For more details
216-
on this controllers format, see :ref:`controller-string-syntax`.
216+
on the controller format, see :ref:`controller-string-syntax`.
217217

218218
.. seealso::
219219

@@ -236,15 +236,12 @@ that is passed to that method::
236236
// ...
237237
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
238238

239-
class HelloController
239+
/**
240+
* @Route("/hello/{name}", name="hello")
241+
*/
242+
public function indexAction($name)
240243
{
241-
/**
242-
* @Route("/hello/{name}", name="hello")
243-
*/
244-
public function indexAction($name)
245-
{
246-
// ...
247-
}
244+
// ...
248245
}
249246

250247
The controller has a single argument, ``$name``, which corresponds to the
@@ -260,6 +257,7 @@ Take the following more-interesting example:
260257
261258
// src/AppBundle/Controller/HelloController.php
262259
// ...
260+
263261
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
264262
265263
class HelloController
@@ -360,7 +358,9 @@ the following guidelines in mind while you develop.
360358

361359
Every route also has a special ``_route`` parameter, which is equal to
362360
the name of the route that was matched (e.g. ``hello``). Though not usually
363-
useful, this is also available as a controller argument.
361+
useful, this is also available as a controller argument. You can also
362+
pass other variables from your route to your controller arguments. See
363+
:doc:`/cookbook/routing/extra_information.`.
364364

365365
.. _book-controller-request-argument:
366366

@@ -369,7 +369,7 @@ The ``Request`` as a Controller Argument
369369

370370
What if you need to read query parameters, grab a request header or get access
371371
to an uploaded file? All of that information is stored in Symfony's ``Request``
372-
object. To get it in your contorller, just add it as an argument and
372+
object. To get it in your controller, just add it as an argument and
373373
**type-hint it with the Request class**::
374374

375375
use Symfony\Component\HttpFoundation\Request;
@@ -384,7 +384,7 @@ object. To get it in your contorller, just add it as an argument and
384384
.. seealso::
385385

386386
Want to know more about getting information from the request? See
387-
:ref:`Access Request Information <component-http-foundation-request`.
387+
:ref:`Access Request Information <component-http-foundation-request>`.
388388

389389
.. index::
390390
single: Controller; Base controller class
@@ -423,17 +423,6 @@ A great way to see the core functionality in action is to look in the
423423
This is optional, but can give you more control over the exact objects/dependencies
424424
that are injected into your controller.
425425

426-
.. index::
427-
single: Controller; Common tasks
428-
429-
Common Controller Tasks
430-
-----------------------
431-
432-
Yes, a controller can do anything. But most controllers do the same basic
433-
tasks over and over again. These tasks - like redirecting, rendering templates
434-
and accessing core services - are easier with the shortcut methods you get
435-
by extending the base ``Controller`` class.
436-
437426
.. index::
438427
single: Controller; Redirecting
439428

@@ -502,19 +491,6 @@ The Symfony templating engine is explained in great detail in the
502491
``AppBundle:Hello:index.html.twig`` would refer to the template located in
503492
``src/AppBundle/Resources/views/Hello/index.html.twig``. See :ref:`template-referencing-in-bundle`.
504493

505-
.. tip::
506-
507-
The ``render`` method is a shortcut to direct use of the ``templating``
508-
service. The ``templating`` service can also be used directly::
509-
510-
$templating = $this->get('templating');
511-
512-
// returns the content
513-
$content = $templating->render('Hello/index.html.twig', array('name' => $name));
514-
515-
// puts the content into a Response object for convenience
516-
$content = $templating->renderResponse('Hello/index.html.twig', array('name' => $name));
517-
518494
.. index::
519495
single: Controller; Accessing services
520496

@@ -526,7 +502,7 @@ Accessing other Services
526502
Symfony comes packed with a lot of useful objects, called services. These
527503
are used for rendering templates, sending emails, querying the database and
528504
any other "work" you can think of. When you install a new bundle, it probably
529-
brings in even *more* services. And of course, will even :ref:`add your own services <service-container-creating-service>`.
505+
brings in even *more* services.
530506

531507
When extending the base controller class, you can access any Symfony service
532508
via the ``get()`` method. Here are several common services you might need::
@@ -705,19 +681,19 @@ content that's sent back to the client::
705681
$response->headers->set('Content-Type', 'application/json');
706682

707683
The ``headers`` property is a :class:`Symfony\\Component\\HttpFoundation\\HeaderBag`
708-
object some nice methods for getting and setting the headers. The header
709-
names are normalized so that using ``Content-Type`` is equivalent to ``content-type``
710-
or even ``content_type``.
684+
object and has some nice methods for getting and setting the headers. The
685+
header names are normalized so that using ``Content-Type`` is equivalent to
686+
``content-type`` or even ``content_type``.
711687

712688
There are also special classes to make certain kinds of responses easier:
713689

714-
- For JSON, there is :class:`Symfony\\Component\\HttpFoundation\\JsonResponse`.
690+
* For JSON, there is :class:`Symfony\\Component\\HttpFoundation\\JsonResponse`.
715691
See :ref:`component-http-foundation-json-response`.
716692

717-
- For files, there is :class:`Symfony\\Component\\HttpFoundation\\BinaryFileResponse`.
693+
* For files, there is :class:`Symfony\\Component\\HttpFoundation\\BinaryFileResponse`.
718694
See :ref:`component-http-foundation-serving-files`.
719695

720-
- For streamed responses, there is :class:`Symfony\\Component\\HttpFoundation\\StreamedResponse`.
696+
* For streamed responses, there is :class:`Symfony\\Component\\HttpFoundation\\StreamedResponse`.
721697
See :ref:`streaming-response`.
722698

723699
.. seealso::
@@ -763,7 +739,7 @@ Creating Static Pages
763739
You can create a static page without even creating a controller (only a route
764740
and template are needed).
765741

766-
Use it! See :doc:`/cookbook/templating/render_without_controller`.
742+
See :doc:`/cookbook/templating/render_without_controller`.
767743

768744
.. index::
769745
single: Controller; Forwarding

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.