HttpFoundation: Request and Response
Wrap superglobals in HttpFoundation Request and build Response objects.
HttpFoundation: Request and Response is a free PHP Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What is HttpFoundation?
Symfony HttpFoundation is a standalone PHP component that wraps the raw HTTP superglobals ($_GET, $_POST, $_SERVER) in clean, OO classes.
Installing HttpFoundation
Install as a standalone package via Composer.
$ composer require symfony/http-foundationCreating a Request from Globals
Capture the current HTTP request from superglobals.
<?php
use Symfony\Component\HttpFoundation\Request;
$request = Request::createFromGlobals();
echo $request->query->get("page", 1); // GET param
echo $request->request->get("email"); // POST param
echo $request->headers->get("User-Agent"); // headerReading Input
Request provides typed bags for different input sources.
<?php
$request->query->get("id"); // $_GET
$request->request->get("name"); // $_POST
$request->attributes->get("key"); // route attributes
$request->cookies->get("token"); // $_COOKIE
$request->files->get("avatar"); // $_FILES
$request->server->get("REMOTE_ADDR"); // $_SERVERRequest Methods
Check the HTTP method and path.
<?php
$request->getMethod(); // "GET", "POST", etc.
$request->isMethod("POST"); // bool
$request->getPathInfo(); // "/api/users/1"
$request->getHost(); // "example.com"
$request->isSecure(); // true if HTTPSBuilding a Response
Create and send an HTTP response.
<?php
use Symfony\Component\HttpFoundation\Response;
$response = new Response("Hello World", 200, [
"Content-Type" => "text/plain",
]);
$response->send();JsonResponse
Return JSON with the correct Content-Type header automatically.
<?php
use Symfony\Component\HttpFoundation\JsonResponse;
$response = new JsonResponse(["status" => "ok", "id" => 42]);
$response->send();RedirectResponse
Redirect the user to another URL.
<?php
use Symfony\Component\HttpFoundation\RedirectResponse;
$response = new RedirectResponse("/dashboard", 302);
$response->send();Setting Headers and Cookies
Add headers and cookies to responses before sending.
<?php
$response->headers->set("X-Rate-Limit", "100");
$response->headers->setCookie(
new Cookie("prefs", "dark", time() + 86400, "/", null, true, true)
);BinaryFileResponse
Stream file downloads efficiently with BinaryFileResponse.
<?php
use Symfony\Component\HttpFoundation\BinaryFileResponse;
$response = new BinaryFileResponse("/path/to/file.pdf");
$response->setContentDisposition("attachment", "report.pdf");
$response->send();StreamedResponse
StreamedResponse sends data as it is generated, ideal for large exports or real-time streaming.
Summary
HttpFoundation wraps superglobals in Request and builds responses with Response, JsonResponse, RedirectResponse. It is the HTTP layer used by both Laravel and Symfony.
Quick Check
Which bag accesses GET query parameters in an HttpFoundation Request?
Frequently asked questions
Is the “HttpFoundation: Request and Response” lesson free?
Yes — the full text of “HttpFoundation: Request and Response” is free to read here on the web, and the PHP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.
What will I learn in “HttpFoundation: Request and Response”?
Wrap superglobals in HttpFoundation Request and build Response objects. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start PHP Academy?
No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “HttpFoundation: Request and Response” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this PHP Academy lesson?
Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- HttpFoundation: Request and Response
- DependencyInjection Container
- EventDispatcher: Decoupled Events
- Console Component: CLI Commands