Symfony_quick_tour_3.4
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Getting Information from the Request<br />
Sometimes your controllers need to access the information related to the user request, such as their<br />
preferred language, IP address or the URL query parameters. To get access to this information, add a new<br />
argument of type Request to the action. The name of this new argument doesn't matter, but it must be<br />
preceded by the Request type in order to work (don't forget to add the new use statement that imports<br />
this Request class):<br />
Listing 3-10<br />
1<br />
2<br />
3<br />
4<br />
5<br />
6<br />
7<br />
8<br />
9<br />
10<br />
11<br />
12<br />
13<br />
14<br />
15<br />
16<br />
17<br />
18<br />
19<br />
20<br />
21<br />
22<br />
23<br />
24<br />
25<br />
26<br />
27<br />
// src/AppBundle/Controller/DefaultController.php<br />
namespace AppBundle\Controller;<br />
use <strong>Symfony</strong>\Bundle\FrameworkBundle\Controller\Controller;<br />
use <strong>Symfony</strong>\Component\HttpFoundation\Request;<br />
use <strong>Symfony</strong>\Component\Routing\Annotation\Route;<br />
class DefaultController extends Controller<br />
{<br />
/**<br />
* @Route("/", name="homepage")<br />
*/<br />
public function indexAction(Request $request)<br />
{<br />
// is it an Ajax request?<br />
$isAjax = $request->isXmlHttpRequest();<br />
}<br />
}<br />
// what's the preferred language of the user?<br />
$language = $request->getPreferredLanguage(array('en', 'fr'));<br />
// get the value of a $_GET parameter<br />
$pageName = $request->query->get('page');<br />
// get the value of a $_POST parameter<br />
$pageName = $request->request->get('page');<br />
In a template, you can also access the Request object via the special app.request variable<br />
automatically provided by <strong>Symfony</strong>:<br />
Listing 3-11<br />
1<br />
2<br />
3<br />
{{ app.request.query.get('page') }}<br />
{{ app.request.request.get('page') }}<br />
Persisting Data in the Session<br />
Even if the HTTP protocol is stateless, <strong>Symfony</strong> provides a nice session object that represents the client<br />
(be it a real person using a browser, a bot, or a web service). Between two requests, <strong>Symfony</strong> stores the<br />
attributes in a cookie by using native PHP sessions.<br />
Storing and retrieving information from the session can be easily achieved from any controller:<br />
Listing 3-12<br />
1<br />
2<br />
3<br />
4<br />
5<br />
6<br />
7<br />
8<br />
9<br />
10<br />
11<br />
use <strong>Symfony</strong>\Component\HttpFoundation\Session\Session;<br />
public function indexAction(Session $session)<br />
{<br />
// store an attribute for reuse during a later user request<br />
$session->set('foo', 'bar');<br />
// get the value of a session attribute<br />
$foo = $session->get('foo');<br />
// use a default value if the attribute doesn't exist<br />
PDF brought to you by<br />
Chapter 3: The Controller | 18<br />
generated on December 4, 2017