@@ -364,42 +364,24 @@ the following guidelines in mind while you develop.
364364 pass other variables from your route to your controller arguments. See
365365 :doc: `/cookbook/routing/extra_information `.
366366
367- .. _book-controller-request-argument :
368-
369- The ``Request `` as a Controller Argument
370- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
371-
372- What if you need to read query parameters, grab a request header or get access
373- to an uploaded file? All of that information is stored in Symfony's ``Request ``
374- object. To get it in your controller, just add it as an argument and
375- **type-hint it with the Request class **::
376-
377- use Symfony\Component\HttpFoundation\Request;
378-
379- public function indexAction($firstName, $lastName, Request $request)
380- {
381- $page = $request->query->get('page', 1);
382-
383- // ...
384- }
385-
386- .. seealso ::
387-
388- Want to know more about getting information from the request? See
389- :ref: `Access Request Information <component-http-foundation-request >`.
390-
391367.. index ::
392368 single: Controller; Base controller class
393369
394370The Base Controller Class
395371-------------------------
396372
397- For convenience, Symfony comes with an optional base ``Controller `` class.
398- If you extend it, you'll get access to a number of helper methods and all
399- of your service objects via the container (see :ref: `controller-accessing-services `).
373+ For convenience, Symfony comes with an optional base
374+ :class: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ Controller ` class.
375+ If you extend it, this wont change anything about how your controller
376+ works, but you'll get access to a number of **helper methods ** and the
377+ **service container ** (see :ref: `controller-accessing-services `): an
378+ array-like object that gives you access to every useful object in the
379+ system. These useful objects are called **services **, and Symfony ships
380+ with a service object that can render Twig templates, another that can
381+ log messages and many more.
400382
401- Add the ``use `` statement atop the ``Controller `` class and then modify the
402- ``HelloController `` to extend it::
383+ Add the ``use `` statement atop the ``Controller `` class and then modify
384+ the ``HelloController `` to extend it::
403385
404386 // src/AppBundle/Controller/HelloController.php
405387 namespace AppBundle\Controller;
@@ -411,39 +393,44 @@ Add the ``use`` statement atop the ``Controller`` class and then modify the
411393 // ...
412394 }
413395
414- This doesn't actually change anything about how your controller works: it
415- just gives you access to helper methods that the base controller class makes
416- available. These are just shortcuts to using core Symfony functionality that's
417- available to you with or without the use of the base ``Controller `` class.
418- A great way to see the core functionality in action is to look in the
419- `Controller class `_.
396+ Helper methods are just shortcuts to using core Symfony functionality
397+ that's available to you with or without the use of the base
398+ ``Controller `` class. A great way to see the core functionality in
399+ action is to look in the
400+ :class: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ Controller ` class.
420401
421402.. seealso ::
422403
423- If you're curious about how a controller would work that did *not * extend
424- this base class, check out :doc: `Controllers as Services </cookbook/controller/service >`.
425- This is optional, but can give you more control over the exact objects/dependencies
426- that are injected into your controller.
404+ If you're curious about how a controller would work that did *not *
405+ extend this base ``Controller `` class, check out cookbook article
406+ :doc: `Controllers as Services </cookbook/controller/service >`.
407+ This is optional, but can give you more control over the exact
408+ objects/dependencies that are injected into your controller.
427409
428410.. index ::
429411 single: Controller; Redirecting
430412
413+ Generating URLs
414+ ~~~~~~~~~~~~~~~
415+
416+ The :method: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ Controller::generateUrl `
417+ method is just a helper method that generates the URL for a given route.
418+
419+ .. _book-redirecting-users-browser :
420+
431421Redirecting
432422~~~~~~~~~~~
433423
434- If you want to redirect the user to another page, use the
424+ To redirect the user's browser to another page **internally **, use the ``generateUrl() ``
425+ method in combination with another helper method called
435426:method: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ Controller::redirect `
436- method ::
427+ which needs URL as an argument ::
437428
438429 public function indexAction()
439430 {
440431 return $this->redirect($this->generateUrl('homepage'));
441432 }
442433
443- The ``generateUrl() `` method is just a helper function that generates the URL
444- for a given route. For more information, see the :doc: `Routing </book/routing >`
445- chapter.
446-
447434By default, the ``redirect() `` method performs a 302 (temporary) redirect. To
448435perform a 301 (permanent) redirect, modify the second argument::
449436
@@ -452,6 +439,15 @@ perform a 301 (permanent) redirect, modify the second argument::
452439 return $this->redirect($this->generateUrl('homepage'), 301);
453440 }
454441
442+ To redirect **externally **, use ``redirect() `` and pass it the external URL::
443+
444+ public function indexAction()
445+ {
446+ return $this->redirect('http://symfony.com/doc');
447+ }
448+
449+ For more information, see the :doc: `Routing chapter </book/routing >`.
450+
455451.. tip ::
456452
457453 The ``redirect() `` method is simply a shortcut that creates a ``Response ``
@@ -469,31 +465,48 @@ perform a 301 (permanent) redirect, modify the second argument::
469465Rendering Templates
470466~~~~~~~~~~~~~~~~~~~
471467
472- If you're serving HTML, you'll want to render a template. The ``render() ``
473- method renders a template **and ** puts that content into a ``Response ``
474- object for you::
468+ To serve HTML, template needs to be rendered and the content put into
469+ the ``Response `` object. The shortcut method
470+ :method: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ Controller::render `
471+ of ``Controller `` class does just that::
475472
476473 // renders app/Resources/views/hello/index.html.twig
477474 return $this->render('hello/index.html.twig', array('name' => $name));
478475
479- You can also put templates in deeper sub-directories. Just try to avoid creating
480- unnecessarily deep structures::
476+ Templates can be also put in deeper sub-directories. Just try to avoid
477+ creating unnecessarily deep structures::
481478
482479 // renders app/Resources/views/hello/greetings/index.html.twig
483480 return $this->render('hello/greetings/index.html.twig', array(
484481 'name' => $name
485482 ));
486483
484+ Templates are a generic way to render content in *any * format. And while in
485+ most cases you'll use templates to render HTML content, a template can just
486+ as easily generate JavaScript, CSS, XML or any other format you can dream of.
487+ To learn how to render different templating formats read :ref: `<template-formats >`
488+ section of the Creating and Using Templates chapter.
489+
487490The Symfony templating engine is explained in great detail in the
488- :doc: `Templating </book/templating >` chapter.
491+ :doc: `Creating and Using Templates chapter </book/templating >`.
492+
493+ .. sidebar :: Templating Naming Pattern
494+
495+ Templates for a bundle can be put in the ``src/path/to/bundle/Resources/views ``
496+ directory of a bundle and reference with a special shortcut syntax called
497+ *logical name * which, for example, looks like ``AppBundle:Hello:index.html.twig `` or
498+ ``AppBundle::layout.html.twig ``. The pattern has three parts, each separated
499+ by a colon. Syntax used to specify a template for a specific page::
489500
490- .. sidebar :: Referencing Templates that Live inside the Bundle
501+ **bundle**:**directory**:**filename**
491502
492- You can also put templates in the ``Resources/views `` directory of a
493- bundle and reference them with a
494- ``BundleName:DirectoryName:FileName `` syntax. For example,
495- ``AppBundle:Hello:index.html.twig `` would refer to the template located in
496- ``src/AppBundle/Resources/views/Hello/index.html.twig ``. See :ref: `template-referencing-in-bundle `.
503+ Syntax used to refer to a base template that's specific to the bundle::
504+
505+ ``**bundle**::**filename**``
506+
507+ For more details on the template format, read
508+ :ref: `template-referencing-in-bundle ` subtitle of the Creating and
509+ Using Templates chapter.
497510
498511.. index ::
499512 single: Controller; Accessing services
@@ -509,7 +522,9 @@ any other "work" you can think of. When you install a new bundle, it probably
509522brings in even *more * services.
510523
511524When extending the base controller class, you can access any Symfony service
512- via the ``get() `` method. Here are several common services you might need::
525+ via the :method: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ Controller::get `
526+ method of the ``Controller `` class. Here are several common services you might
527+ need::
513528
514529 $templating = $this->get('templating');
515530
@@ -518,7 +533,7 @@ via the ``get()`` method. Here are several common services you might need::
518533 $mailer = $this->get('mailer');
519534
520535What other services exist? To list all services, use the ``container:debug ``
521- console command:
536+ console command::
522537
523538.. code-block :: bash
524539
@@ -535,7 +550,7 @@ Managing Errors and 404 Pages
535550
536551When things are not found, you should play well with the HTTP protocol and
537552return a 404 response. To do this, you'll throw a special type of exception.
538- If you're extending the base controller class, do the following::
553+ If you're extending the base `` Controller `` class, do the following::
539554
540555 public function indexAction()
541556 {
@@ -548,8 +563,9 @@ If you're extending the base controller class, do the following::
548563 return $this->render(...);
549564 }
550565
551- The ``createNotFoundException() `` method is just a shortcut to create a
552- special :class: `Symfony\\ Component\\ HttpKernel\\ Exception\\ NotFoundHttpException `
566+ The :method: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ Controller::createNotFoundException `
567+ method is just a shortcut to create a special
568+ :class: `Symfony\\ Component\\ HttpKernel\\ Exception\\ NotFoundHttpException `
553569object, which ultimately triggers a 404 HTTP response inside Symfony.
554570
555571Of course, you're free to throw any ``Exception `` class in your controller -
@@ -560,16 +576,35 @@ Symfony will automatically return a 500 HTTP response code.
560576 throw new \Exception('Something went wrong!');
561577
562578 In every case, an error page is shown to the end user and a full debug
563- error page is shown to the developer (i.e. when you're using ``app_dev.php `` -
564- see :ref: `page-creation-environments `).
579+ error page is shown to the developer (i.e. when you're using ``app_dev.php ``
580+ front controller - see :ref: `page-creation-environments `).
565581
566- You'll want to customize the error page your user sees. To do that, see the
567- ":doc: `/cookbook/controller/error_pages `" cookbook recipe.
582+ You'll want to customize the error page your user sees. To do that, see
583+ the cookbook article ":doc: `/cookbook/controller/error_pages `" cookbook recipe.
568584
569585.. index ::
570586 single: Controller; The session
571587 single: Session
572588
589+ .. _book-controller-request-argument :
590+
591+ The Request object as a Controller Argument
592+ -------------------------------------------
593+
594+ What if you need to read query parameters, grab a request header or get access
595+ to an uploaded file? All of that information is stored in Symfony's ``Request ``
596+ object. To get it in your controller, just add it as an argument and
597+ **type-hint it with the ``Request`` class **::
598+
599+ use Symfony\Component\HttpFoundation\Request;
600+
601+ public function indexAction($firstName, $lastName, Request $request)
602+ {
603+ $page = $request->query->get('page', 1);
604+
605+ // ...
606+ }
607+
573608Managing the Session
574609--------------------
575610
@@ -578,8 +613,11 @@ about the user (be it a real person using a browser, a bot, or a web service)
578613between requests. By default, Symfony stores the attributes in a cookie
579614by using the native PHP sessions.
580615
581- Storing and retrieving information from the session can be easily achieved
582- from any controller::
616+ To retrieving the session we have to call the
617+ :method: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ Controller::getSession `
618+ method on the ``Request `` object inside a controller. Method returns a
619+ :class: `Symfony\\ Component\\ HttpFoundation\\ Session\\ SessionInterface `
620+ with all the methods to handle a session::
583621
584622 use Symfony\Component\HttpFoundation\Request;
585623
@@ -597,8 +635,7 @@ from any controller::
597635 $filters = $session->get('filters', array());
598636 }
599637
600- These attributes will remain in the session for the remainder of that user's
601- session.
638+ Stored attributes remain in the session for the remainder of that user's session.
602639
603640.. index ::
604641 single: Session; Flash messages
@@ -835,6 +872,4 @@ Learn more from the Cookbook
835872----------------------------
836873
837874* :doc: `/cookbook/controller/error_pages `
838- * :doc: `/cookbook/controller/service `
839-
840- .. _`Controller class` : https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php
875+ * :doc: `/cookbook/controller/service `
0 commit comments