Request and Response In Symfony:-
As we have already talked about the request in simple php as this picture will explain it to you and why do we use the headers to make the browsers understand what it has to render and
show to the end user .
so these two pictures clearly shows how the request response works for simple vanilla php now lets come to the symfony part .
The thing is Symfony provides an alternative way of dealing with the help of two classes which are simple object oriented for the betterment which are Request and Response .

To be specific the source code is here and the explanation is further given :-
use Symfony\Component\HttpFoundation\Request;
$request = Request::createFromGlobals();
// the URI being requested (e.g. /about) minus any query parameters
$request->getPathInfo();
// retrieve GET and POST variables respectively
$request->query->get('foo');
$request->request->get('bar', 'default value if bar does not exist');
// retrieve SERVER variables
$request->server->get('HTTP_HOST');
// retrieves an instance of UploadedFile identified by foo
$request->files->get('foo');
// retrieve a COOKIE value
$request->cookies->get('PHPSESSID');
// retrieve an HTTP request header, with normalized, lowercase keys
$request->headers->get('host');
$request->headers->get('content_type');
$request->getMethod(); // GET, POST, PUT, DELETE, HEAD
$request->getLanguages();
what is done here is a special class that is request object is taken from the symfony library which contains of all the parameters in the get and post and whatever comes to the particluar api and with the use of this Request object we can take our variables .
In the common scenario how it is done is :-
example , if a json is coming to the function as a request you will fetch all the details like this :-
$json_data = json_decode($request->getContent(), true);
if you want to get all the post parameters that came inside the request object then you simple fetch it by this code .
if you want to get all the post parameters that came inside the request object then you simple fetch it by this code .
$postParams = (array) $request->request->all();
you can get the headers used when sending the requests , get the languages which can be used , the methid used to send the request using code which the most generally used and the best practices used here .
// retrieve an HTTP request header, with normalized, lowercase keys
$request->headers->get('host');
$request->headers->get('content_type');
$request->getMethod(); // GET, POST, PUT, DELETE, HEAD
$request->getLanguages();
These kind of method attributes are called as the parameter objects .
ParameterBags:-
As we used query here , its a parameter object which has methods like,
has()
all()
get()
and so on .
Response in Symfony :-
In the same way symfony provides you with the simple object oriented style of class of the Response . You can include it in the use statements and use it inside your code wherever you want it .
As you can easily make out an understanding from the pictures here ,
No comments:
Post a Comment