-
- Notifications
You must be signed in to change notification settings - Fork 5.3k
Completely re-reading the controller chapter #4433
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
| @@ -202,18 +202,18 @@ to the controller: | |
return $collection; | ||
| ||
Now, you can go to ``/hello/ryan`` (e.g. ``http://localhost:8000/app_dev.php/hello/ryan`` | ||
if you're using the :doc:`built-in web server </cookbook/webserver/built_in>`_) | ||
if you're using the :doc:`built-in web server </cookbook/webserver/built_in>`) | ||
and Symfony will execute the ``HelloController::indexAction()`` controller | ||
and pass in ``ryan`` for the ``$name`` variable. Creating a "page" means | ||
simply creating a controller method and associated route. | ||
simply creating a controller method and an associated route. | ||
| ||
Simple, right? | ||
| ||
.. sidebar:: The AppBundle:Hello:index syntax for YML and XML | ||
.. sidebar:: The AppBundle:Hello:index controller syntax | ||
| ||
If you use the YML or XML formats, you'll refer to the controller using | ||
a special shortcut syntax: ``AppBundle:Hello:index``. For more details | ||
on this controllers format, see :ref:`controller-string-syntax`. | ||
on the controller format, see :ref:`controller-string-syntax`. | ||
| ||
.. seealso:: | ||
| ||
| @@ -236,15 +236,12 @@ that is passed to that method:: | |
// ... | ||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | ||
| ||
class HelloController | ||
/** | ||
* @Route("/hello/{name}", name="hello") | ||
*/ | ||
public function indexAction($name) | ||
{ | ||
/** | ||
* @Route("/hello/{name}", name="hello") | ||
*/ | ||
public function indexAction($name) | ||
{ | ||
// ... | ||
} | ||
// ... | ||
} | ||
| ||
The controller has a single argument, ``$name``, which corresponds to the | ||
| @@ -260,6 +257,7 @@ Take the following more-interesting example: | |
| ||
// src/AppBundle/Controller/HelloController.php | ||
// ... | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there should be an empty line between a file comment and a code/folding comment | ||
| ||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | ||
| ||
class HelloController | ||
| @@ -360,7 +358,9 @@ the following guidelines in mind while you develop. | |
| ||
Every route also has a special ``_route`` parameter, which is equal to | ||
the name of the route that was matched (e.g. ``hello``). Though not usually | ||
useful, this is also available as a controller argument. | ||
useful, this is also available as a controller argument. You can also | ||
pass other variables from your route to your controller arguments. See | ||
:doc:`/cookbook/routing/extra_information.`. | ||
| ||
.. _book-controller-request-argument: | ||
| ||
| @@ -369,7 +369,7 @@ The ``Request`` as a Controller Argument | |
| ||
What if you need to read query parameters, grab a request header or get access | ||
to an uploaded file? All of that information is stored in Symfony's ``Request`` | ||
object. To get it in your contorller, just add it as an argument and | ||
object. To get it in your controller, just add it as an argument and | ||
**type-hint it with the Request class**:: | ||
| ||
use Symfony\Component\HttpFoundation\Request; | ||
| @@ -384,7 +384,7 @@ object. To get it in your contorller, just add it as an argument and | |
.. seealso:: | ||
| ||
Want to know more about getting information from the request? See | ||
:ref:`Access Request Information <component-http-foundation-request`. | ||
:ref:`Access Request Information <component-http-foundation-request>`. | ||
| ||
.. index:: | ||
single: Controller; Base controller class | ||
| @@ -423,17 +423,6 @@ A great way to see the core functionality in action is to look in the | |
This is optional, but can give you more control over the exact objects/dependencies | ||
that are injected into your controller. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. even if you didn't extend the base class, you could still use it as controllers that do extend the base class. See the examples in the beginning of this article as an example. I think this should be reworded. (as you are better in that than me, I'll leave the exact words up to you) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you don't extend the base class, then you're either registering your controller as a service or you're implementing So I was thinking that there are 2 99% use-cases: extending the base controller and registering your controller as a service. That's why I used this wording. With all of this, would you still like to have some part of this changed/emphasized better? Let me know - I started to re-phrase it, but then realized I wanted a bit more input :). | ||
| ||
.. index:: | ||
single: Controller; Common tasks | ||
| ||
Common Controller Tasks | ||
----------------------- | ||
| ||
Yes, a controller can do anything. But most controllers do the same basic | ||
tasks over and over again. These tasks - like redirecting, rendering templates | ||
and accessing core services - are easier with the shortcut methods you get | ||
by extending the base ``Controller`` class. | ||
| ||
.. index:: | ||
single: Controller; Redirecting | ||
| ||
| @@ -502,19 +491,6 @@ The Symfony templating engine is explained in great detail in the | |
``AppBundle:Hello:index.html.twig`` would refer to the template located in | ||
``src/AppBundle/Resources/views/Hello/index.html.twig``. See :ref:`template-referencing-in-bundle`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The positions of the line breaks look a bit weird. | ||
| ||
.. tip:: | ||
| ||
The ``render`` method is a shortcut to direct use of the ``templating`` | ||
service. The ``templating`` service can also be used directly:: | ||
| ||
$templating = $this->get('templating'); | ||
| ||
// returns the content | ||
$content = $templating->render('Hello/index.html.twig', array('name' => $name)); | ||
| ||
// puts the content into a Response object for convenience | ||
$content = $templating->renderResponse('Hello/index.html.twig', array('name' => $name)); | ||
| ||
.. index:: | ||
single: Controller; Accessing services | ||
| ||
| @@ -526,7 +502,7 @@ Accessing other Services | |
Symfony comes packed with a lot of useful objects, called services. These | ||
are used for rendering templates, sending emails, querying the database and | ||
any other "work" you can think of. When you install a new bundle, it probably | ||
brings in even *more* services. And of course, will even :ref:`add your own services <service-container-creating-service>`. | ||
brings in even *more* services. | ||
| ||
When extending the base controller class, you can access any Symfony service | ||
via the ``get()`` method. Here are several common services you might need:: | ||
| @@ -705,19 +681,19 @@ content that's sent back to the client:: | |
$response->headers->set('Content-Type', 'application/json'); | ||
| ||
The ``headers`` property is a :class:`Symfony\\Component\\HttpFoundation\\HeaderBag` | ||
object some nice methods for getting and setting the headers. The header | ||
names are normalized so that using ``Content-Type`` is equivalent to ``content-type`` | ||
or even ``content_type``. | ||
object and has some nice methods for getting and setting the headers. The | ||
header names are normalized so that using ``Content-Type`` is equivalent to | ||
``content-type`` or even ``content_type``. | ||
| ||
There are also special classes to make certain kinds of responses easier: | ||
| ||
- For JSON, there is :class:`Symfony\\Component\\HttpFoundation\\JsonResponse`. | ||
* For JSON, there is :class:`Symfony\\Component\\HttpFoundation\\JsonResponse`. | ||
See :ref:`component-http-foundation-json-response`. | ||
| ||
- For files, there is :class:`Symfony\\Component\\HttpFoundation\\BinaryFileResponse`. | ||
* For files, there is :class:`Symfony\\Component\\HttpFoundation\\BinaryFileResponse`. | ||
See :ref:`component-http-foundation-serving-files`. | ||
| ||
- For streamed responses, there is :class:`Symfony\\Component\\HttpFoundation\\StreamedResponse`. | ||
* For streamed responses, there is :class:`Symfony\\Component\\HttpFoundation\\StreamedResponse`. | ||
See :ref:`streaming-response`. | ||
| ||
.. seealso:: | ||
| @@ -763,7 +739,7 @@ Creating Static Pages | |
You can create a static page without even creating a controller (only a route | ||
and template are needed). | ||
| ||
Use it! See :doc:`/cookbook/templating/render_without_controller`. | ||
See :doc:`/cookbook/templating/render_without_controller`. | ||
| ||
.. index:: | ||
single: Controller; Forwarding | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are missing the underscore here (
web_server
instead ofwebserver
).