Google News
logo
Symfony - Interview Questions
What is the Symfony Request? and Explain the
Request : The most common way to create a request is to base it on the current PHP global variables with createFromGlobals():
use Symfony\Component\HttpFoundation\Request;

$request = Request::createFromGlobals();
 
Which is almost equivalent to the more verbose, but also more flexible, __construct() call :
$request = new Request(
    $_GET,
    $_POST,
    [],
    $_COOKIE,
    $_FILES,
    $_SERVER
);
 
Accessing Request Data : A Request object holds information about the client request. This information can be accessed via several public properties :
 
request : equivalent of $_POST;
query : equivalent of $_GET ($request->query->get('name'));
cookies : equivalent of $_COOKIE;
attributes : no equivalent - used by your app to store other data (see below);
files : equivalent of $_FILES;
server : equivalent of $_SERVER;
headers : mostly equivalent to a subset of $_SERVER ($request->headers->get('User-Agent')).


Identifying a Request : In your application, you need a way to identify a request; most of the time, this is done via the "path info" of the request, which can be accessed via the getPathInfo() method :
// for a request to http://example.com/blog/index.php/post/hello-world
// the path info is "/post/hello-world"
$request->getPathInfo();
 
Simulating a Request : Instead of creating a request based on the PHP globals, you can also simulate a request :
$request = Request::create(
    '/hello-world',
    'GET',
    ['name' => 'Fabien']
);
Advertisement