| name | symfony-7-4-http-foundation |
| description | Symfony 7.4 HttpFoundation component reference. Triggers on: HttpFoundation, Request, Response, Session, Cookie, File upload, ParameterBag, HeaderBag, JsonResponse, RedirectResponse, StreamedResponse, BinaryFileResponse, InputBag, FileBag, ServerBag, AcceptHeader, IpUtils, HeaderUtils, StreamedJsonResponse, EventStreamResponse, ServerEvent, UrlHelper. |
Symfony 7.4 HttpFoundation
Overview
The HttpFoundation component replaces PHP's native superglobals ($_GET, $_POST, $_COOKIE, $_FILES, $_SERVER) and functions (header(), setcookie(), echo) with an object-oriented API for HTTP request/response handling.
Quick Reference
Request
use Symfony\Component\HttpFoundation\Request;
$request = Request::createFromGlobals();
$request = Request::create('/path', 'POST', ['key' => 'value']);
$request->query->get('page', 1);
$request->request->get('name');
$request->cookies->get('token');
$request->files->get('upload');
$request->server->get('HTTP_HOST');
$request->headers->get('Content-Type');
$request->attributes->get('_route');
$request->getMethod();
$request->getPathInfo();
$request->getContent();
$request->toArray();
$request->getPayload();
Response
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\HttpFoundation\StreamedJsonResponse;
$response = new Response('Hello', Response::HTTP_OK, ['Content-Type' => 'text/html']);
$response = new JsonResponse(['data' => 123]);
$response = new RedirectResponse('https://example.com/');
$response = new BinaryFileResponse('/path/to/file.pdf');
$response = new StreamedResponse(function () { echo 'chunk'; flush(); });
$response = new StreamedJsonResponse(['items' => $generator]);
Cookie
use Symfony\Component\HttpFoundation\Cookie;
$cookie = Cookie::create('name')
->withValue('value')
->withExpires(strtotime('+1 day'))
->withDomain('.example.com')
->withSecure(true)
->withHttpOnly(true)
->withSameSite(Cookie::SAMESITE_LAX);
$response->headers->setCookie($cookie);
$response->headers->clearCookie('name');
Cache Headers
$response->setPublic();
$response->setMaxAge(3600);
$response->setSharedMaxAge(600);
$response->setETag('abc');
$response->setLastModified(new \DateTime());
$response->setVary(['Accept-Encoding']);
Full Documentation
Full Documentation
See references/http-foundation.md for complete API documentation including ParameterBag methods, HeaderUtils, IpUtils, AcceptHeader, request matchers, SSE, and all response types.