訪問控制器中的請求

<?php

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)
   {
      //BASICS

      //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();
   }
}

請注意,注入的 Request 物件適用於當前請求(它可能或可能不等於主請求)。