@@ -364,42 +364,24 @@ the following guidelines in mind while you develop.
364
364
pass other variables from your route to your controller arguments. See
365
365
:doc: `/cookbook/routing/extra_information `.
366
366
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
-
391
367
.. index ::
392
368
single: Controller; Base controller class
393
369
394
370
The Base Controller Class
395
371
-------------------------
396
372
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.
400
382
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::
403
385
404
386
// src/AppBundle/Controller/HelloController.php
405
387
namespace AppBundle\Controller;
@@ -411,39 +393,44 @@ Add the ``use`` statement atop the ``Controller`` class and then modify the
411
393
// ...
412
394
}
413
395
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.
420
401
421
402
.. seealso ::
422
403
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.
427
409
428
410
.. index ::
429
411
single: Controller; Redirecting
430
412
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
+
431
421
Redirecting
432
422
~~~~~~~~~~~
433
423
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
435
426
:method: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ Controller::redirect `
436
- method ::
427
+ which needs URL as an argument ::
437
428
438
429
public function indexAction()
439
430
{
440
431
return $this->redirect($this->generateUrl('homepage'));
441
432
}
442
433
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
-
447
434
By default, the ``redirect() `` method performs a 302 (temporary) redirect. To
448
435
perform a 301 (permanent) redirect, modify the second argument::
449
436
@@ -452,6 +439,15 @@ perform a 301 (permanent) redirect, modify the second argument::
452
439
return $this->redirect($this->generateUrl('homepage'), 301);
453
440
}
454
441
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
+
455
451
.. tip ::
456
452
457
453
The ``redirect() `` method is simply a shortcut that creates a ``Response ``
@@ -469,31 +465,48 @@ perform a 301 (permanent) redirect, modify the second argument::
469
465
Rendering Templates
470
466
~~~~~~~~~~~~~~~~~~~
471
467
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::
475
472
476
473
// renders app/Resources/views/hello/index.html.twig
477
474
return $this->render('hello/index.html.twig', array('name' => $name));
478
475
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::
481
478
482
479
// renders app/Resources/views/hello/greetings/index.html.twig
483
480
return $this->render('hello/greetings/index.html.twig', array(
484
481
'name' => $name
485
482
));
486
483
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
+
487
490
The 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::
489
500
490
- .. sidebar :: Referencing Templates that Live inside the Bundle
501
+ **bundle**:**directory**:**filename**
491
502
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.
497
510
498
511
.. index ::
499
512
single: Controller; Accessing services
@@ -509,7 +522,9 @@ any other "work" you can think of. When you install a new bundle, it probably
509
522
brings in even *more * services.
510
523
511
524
When 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::
513
528
514
529
$templating = $this->get('templating');
515
530
@@ -518,7 +533,7 @@ via the ``get()`` method. Here are several common services you might need::
518
533
$mailer = $this->get('mailer');
519
534
520
535
What other services exist? To list all services, use the ``container:debug ``
521
- console command:
536
+ console command::
522
537
523
538
.. code-block :: bash
524
539
@@ -535,7 +550,7 @@ Managing Errors and 404 Pages
535
550
536
551
When things are not found, you should play well with the HTTP protocol and
537
552
return 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::
539
554
540
555
public function indexAction()
541
556
{
@@ -548,8 +563,9 @@ If you're extending the base controller class, do the following::
548
563
return $this->render(...);
549
564
}
550
565
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 `
553
569
object, which ultimately triggers a 404 HTTP response inside Symfony.
554
570
555
571
Of 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.
560
576
throw new \Exception('Something went wrong!');
561
577
562
578
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 `).
565
581
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.
568
584
569
585
.. index ::
570
586
single: Controller; The session
571
587
single: Session
572
588
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
+
573
608
Managing the Session
574
609
--------------------
575
610
@@ -578,8 +613,11 @@ about the user (be it a real person using a browser, a bot, or a web service)
578
613
between requests. By default, Symfony stores the attributes in a cookie
579
614
by using the native PHP sessions.
580
615
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::
583
621
584
622
use Symfony\Component\HttpFoundation\Request;
585
623
@@ -597,8 +635,7 @@ from any controller::
597
635
$filters = $session->get('filters', array());
598
636
}
599
637
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.
602
639
603
640
.. index ::
604
641
single: Session; Flash messages
@@ -835,6 +872,4 @@ Learn more from the Cookbook
835
872
----------------------------
836
873
837
874
* :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