Skip to content

Commit d31465b

Browse files
committed
Merge branch '2.4'
2 parents 34551d2 + 7c1b2c3 commit d31465b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+1204
-582
lines changed

book/controller.rst

+27-17
Original file line numberDiff line numberDiff line change
@@ -590,8 +590,6 @@ Accessing other Services
590590
When extending the base controller class, you can access any Symfony2 service
591591
via the ``get()`` method. Here are several common services you might need::
592592

593-
$request = $this->getRequest();
594-
595593
$templating = $this->get('templating');
596594

597595
$router = $this->get('router');
@@ -660,16 +658,21 @@ by using the native PHP sessions.
660658
Storing and retrieving information from the session can be easily achieved
661659
from any controller::
662660

663-
$session = $this->getRequest()->getSession();
661+
use Symfony\Component\HttpFoundation\Request;
662+
663+
public function indexAction(Request $request)
664+
{
665+
$session = $request->getSession();
664666

665-
// store an attribute for reuse during a later user request
666-
$session->set('foo', 'bar');
667+
// store an attribute for reuse during a later user request
668+
$session->set('foo', 'bar');
667669

668-
// in another controller for another request
669-
$foo = $session->get('foo');
670+
// in another controller for another request
671+
$foo = $session->get('foo');
670672

671-
// use a default value if the key doesn't exist
672-
$filters = $session->get('filters', array());
673+
// use a default value if the key doesn't exist
674+
$filters = $session->get('filters', array());
675+
}
673676

674677
These attributes will remain on the user for the remainder of that user's
675678
session.
@@ -687,11 +690,13 @@ These types of messages are called "flash" messages.
687690

688691
For example, imagine you're processing a form submit::
689692

690-
public function updateAction()
693+
use Symfony\Component\HttpFoundation\Request;
694+
695+
public function updateAction(Request $request)
691696
{
692697
$form = $this->createForm(...);
693698

694-
$form->handleRequest($this->getRequest());
699+
$form->handleRequest($request);
695700

696701
if ($form->isValid()) {
697702
// do some sort of processing
@@ -783,17 +788,22 @@ The Request Object
783788
------------------
784789

785790
Besides the values of the routing placeholders, the controller also has access
786-
to the ``Request`` object when extending the base ``Controller`` class::
791+
to the ``Request`` object. The framework injects the ``Request`` object in the
792+
controller if a variable is type-hinted with
793+
`Symfony\Component\HttpFoundation\Request`::
787794

788-
$request = $this->getRequest();
795+
use Symfony\Component\HttpFoundation\Request;
789796

790-
$request->isXmlHttpRequest(); // is it an Ajax request?
797+
public function indexAction(Request $request)
798+
{
799+
$request->isXmlHttpRequest(); // is it an Ajax request?
791800

792-
$request->getPreferredLanguage(array('en', 'fr'));
801+
$request->getPreferredLanguage(array('en', 'fr'));
793802

794-
$request->query->get('page'); // get a $_GET parameter
803+
$request->query->get('page'); // get a $_GET parameter
795804

796-
$request->request->get('page'); // get a $_POST parameter
805+
$request->request->get('page'); // get a $_POST parameter
806+
}
797807

798808
Like the ``Response`` object, the request headers are stored in a ``HeaderBag``
799809
object and are easily accessible.

book/http_cache.rst

+11-6
Original file line numberDiff line numberDiff line change
@@ -557,12 +557,14 @@ each ``ETag`` must be unique across all representations of the same resource.
557557

558558
To see a simple implementation, generate the ETag as the md5 of the content::
559559

560-
public function indexAction()
560+
use Symfony\Component\HttpFoundation\Request;
561+
562+
public function indexAction(Request $request)
561563
{
562564
$response = $this->render('MyBundle:Main:index.html.twig');
563565
$response->setETag(md5($response->getContent()));
564566
$response->setPublic(); // make sure the response is public/cacheable
565-
$response->isNotModified($this->getRequest());
567+
$response->isNotModified($request);
566568

567569
return $response;
568570
}
@@ -604,7 +606,9 @@ For instance, you can use the latest update date for all the objects needed to
604606
compute the resource representation as the value for the ``Last-Modified``
605607
header value::
606608

607-
public function showAction($articleSlug)
609+
use Symfony\Component\HttpFoundation\Request;
610+
611+
public function showAction($articleSlug, Request $request)
608612
{
609613
// ...
610614

@@ -617,7 +621,7 @@ header value::
617621
// Set response as public. Otherwise it will be private by default.
618622
$response->setPublic();
619623

620-
if ($response->isNotModified($this->getRequest())) {
624+
if ($response->isNotModified($request)) {
621625
return $response;
622626
}
623627

@@ -653,8 +657,9 @@ the better. The ``Response::isNotModified()`` method does exactly that by
653657
exposing a simple and efficient pattern::
654658

655659
use Symfony\Component\HttpFoundation\Response;
660+
use Symfony\Component\HttpFoundation\Request;
656661

657-
public function showAction($articleSlug)
662+
public function showAction($articleSlug, Request $request)
658663
{
659664
// Get the minimum information to compute
660665
// the ETag or the Last-Modified value
@@ -671,7 +676,7 @@ exposing a simple and efficient pattern::
671676
$response->setPublic();
672677

673678
// Check that the Response is not modified for the given Request
674-
if ($response->isNotModified($this->getRequest())) {
679+
if ($response->isNotModified($request)) {
675680
// return the 304 Response immediately
676681
return $response;
677682
} else {

0 commit comments

Comments
 (0)