Symfony2 Request

From WikiOD

Remarks[edit | edit source]

API documentation links (master):

Request object contains several significant data like current Locale and matched Controller. You can use and manage them by HttpKernel events. For reliable understanding of Request-Responce live cycle read this HttpKernel Component doc page (very helpful!).

Access to Request in a Controller[edit | edit source]


use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class TestController extends Controller
   //Inject Request HTTP Component in your function then able to exploit it
   public function myFunctionAction(Request $request)

      //retrieve $_POST variables from request
      $postRequest = $request->request->get('my_data');
      //retrieve $_GET variables from request
      $getRequest = $request->query->get('my_data');
      //get current locale
      $locale = $request->getLocale();

Note that injected Request object applies to current request (it may or may not equal to master request).

Access to Request in a Twig or PHP template.[edit | edit source]

In Twig template, Request object is available at

{{ app.request }}

When you want display request method in Twig, try this:

<p>Request method: {{ app.request.method }}</p>

In PHP template

<p>Request method: <?php echo $app->getRequest()->getMethod() ?></p>