Skip to content

Commit

Permalink
Merge branch '2.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
weaverryan committed Dec 26, 2013
2 parents 34551d2 + 7c1b2c3 commit d31465b
Show file tree
Hide file tree
Showing 56 changed files with 1,204 additions and 582 deletions.
44 changes: 27 additions & 17 deletions book/controller.rst
Original file line number Diff line number Diff line change
Expand Up @@ -590,8 +590,6 @@ Accessing other Services
When extending the base controller class, you can access any Symfony2 service
via the ``get()`` method. Here are several common services you might need::

$request = $this->getRequest();

$templating = $this->get('templating');

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

$session = $this->getRequest()->getSession();
use Symfony\Component\HttpFoundation\Request;

public function indexAction(Request $request)
{
$session = $request->getSession();

// store an attribute for reuse during a later user request
$session->set('foo', 'bar');
// store an attribute for reuse during a later user request
$session->set('foo', 'bar');

// in another controller for another request
$foo = $session->get('foo');
// in another controller for another request
$foo = $session->get('foo');

// use a default value if the key doesn't exist
$filters = $session->get('filters', array());
// use a default value if the key doesn't exist
$filters = $session->get('filters', array());
}

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

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

public function updateAction()
use Symfony\Component\HttpFoundation\Request;

public function updateAction(Request $request)
{
$form = $this->createForm(...);

$form->handleRequest($this->getRequest());
$form->handleRequest($request);

if ($form->isValid()) {
// do some sort of processing
Expand Down Expand Up @@ -783,17 +788,22 @@ The Request Object
------------------

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

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

$request->isXmlHttpRequest(); // is it an Ajax request?
public function indexAction(Request $request)
{
$request->isXmlHttpRequest(); // is it an Ajax request?

$request->getPreferredLanguage(array('en', 'fr'));
$request->getPreferredLanguage(array('en', 'fr'));

$request->query->get('page'); // get a $_GET parameter
$request->query->get('page'); // get a $_GET parameter

$request->request->get('page'); // get a $_POST parameter
$request->request->get('page'); // get a $_POST parameter
}

Like the ``Response`` object, the request headers are stored in a ``HeaderBag``
object and are easily accessible.
Expand Down
17 changes: 11 additions & 6 deletions book/http_cache.rst
Original file line number Diff line number Diff line change
Expand Up @@ -557,12 +557,14 @@ each ``ETag`` must be unique across all representations of the same resource.

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

public function indexAction()
use Symfony\Component\HttpFoundation\Request;

public function indexAction(Request $request)
{
$response = $this->render('MyBundle:Main:index.html.twig');
$response->setETag(md5($response->getContent()));
$response->setPublic(); // make sure the response is public/cacheable
$response->isNotModified($this->getRequest());
$response->isNotModified($request);

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

public function showAction($articleSlug)
use Symfony\Component\HttpFoundation\Request;

public function showAction($articleSlug, Request $request)
{
// ...

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

if ($response->isNotModified($this->getRequest())) {
if ($response->isNotModified($request)) {
return $response;
}

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

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;

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

// Check that the Response is not modified for the given Request
if ($response->isNotModified($this->getRequest())) {
if ($response->isNotModified($request)) {
// return the 304 Response immediately
return $response;
} else {
Expand Down
Loading

0 comments on commit d31465b

Please sign in to comment.