So I’ve been talking a lot about different components Symfony offers. In case you’re not familiar with the component term in the Symfony ecosystem, you just have to know that components are standalone libraries you can install in your project, without having your project to be entirely written with Symfony. You can simply add one Symfony component in your project if a feature interests you. The Symfony framework uses all of these different components to give you a pleasant developer experience.
Among those dozens of components, 3 are an absolute focus on HTTP. Given their name (HttpFoundation, HttpKernel and HttpClient), it may be confusing if you’re new to the Symfony ecosystem. Each of them solves specific needs. Because we could wonder: why don’t the Symfony core team put all HTTP stuff in one and only one “Http” component? Let’s see how using this trio could definitely change the way you deal with HTTP in your applications and projects.
Foundation, Kernel and Client
1. The HttpFoundation component defines an object-oriented layer to manipulate what makes the foundations of HTTP (which isn’t a big surprise). But what do I mean when talking about foundations of HTTP?
You surely know that HTTP is based on on a Request-Response schema. A client sends a request to a server, then the server returns a response. This component defines classes and interfaces to manipulates those requests and responses the object-oriented way. Instead of having to deal with PHP superglobals like
$_COOKIE and so on, you’ll be able to use classes like
Request and do the same thing.
These classes comes with handy methods and helpers to retrieve information from the request, manipulate cache rules, etc. Moreover, you can easily create streamed responses as well as JSON responses (with headers automatically set) for instance. The component also comes with an util to anonymize IP addresses.