Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Kévin Dunglas:
Using PSR-7 in Symfony
Jun 24, 2015 @ 17:50:56

With the recent acceptance of the PSR-7 HTTP standard by the PHP-FIG, there's been a lot of articles about using it in various PHP frameworks. In this new post Kevin Douglas looks at the use of it in Symfony, how it relates to the HttpFoundation component and when it will be included in the framework itself.

Back in 2011, Symfony 2 introduced the HttpFoundation component, a PHP library representing HTTP messages with an object oriented API. HttpFoundation is a key in the success of the HTTP-centric approach of Symfony, and it definitely inspirited the PSR-7 specification. However, PSR-7 and HttpFoundation differ fundamentally in two aspects: PSR-7 messages are immutable, mutability is in the DNA of HttpFoundation and in PSR-7, almost everything is stream.

Because of immutability it is very hard to make HttpFoundation embracing PSR-7 without a huge backward compatibility break impacting thousands of existing applications and bundles.

Work was almost immediately started to support the PSR-7 specification in Symfony, however. As a result support will be ready to be included in Symfony 2.7 but, as the rest of the post shows, it can be introduced in versions 2.3 or greater through a "HTTP message bridge" library. He shows how to get this installed in your Symfony application instance and how to use it in your controllers to interact with Requests and Responses. He does point out, though, that while this can bring your release up to PSR-7 status it comes with some overhead that may not be worth it if you're concerned about performance.

tagged: psr7 symfony bridge httpfoundation performance library

Link: http://dunglas.fr/2015/06/using-psr-7-in-symfony/

Symfony Blog:
PSR-7 Support in Symfony is Here
Jun 01, 2015 @ 17:19:15

The Symfony project has officially announced PSR-7 support in the latest version of the framework. PSR-7 is a recently approved standard by the PHP-FIG to make a more structured HTTP request and response structure (to aid in interoperability).

Less than 2 weeks ago, the PHP community roundly accepted PSR-7, giving PHP a common set of HTTP Message Interfaces. This has huge potential for interoperability and standardization across all of PHP. This is especially true for middleware: functions that hook into the request-response process. In the future, a middleware written around these new interfaces could be used in any framework. [...] Today, a huge number of projects use Symfony's Request and Response classes (via the HttpFoundation component), including Laravel, Drupal 8 and StackPHP.

[...] For that reason, we're thrilled to announce the 0.1 release of the PSR HTTP Message Bridge: a library that can convert Symfony Request and Response objects to PSR-7 compatible objects and back. This means that once there are middleware written for PSR-7, applications using HttpFoundation will be compatible.

The bridge makes it simpler to swap out the HTTP layer by converting the HTTP objects into something other frameworks can use (or so others can be used by Symfony). They provide some examples of how to put it to use, converting objects both to and from the standard Symfony HttpFoundation versions. There's also a quick note about the RequestInterface and ResponseInterface structure that allows you to bridge your own gaps between the PSR-7 friendly components and Symfony.

tagged: psr7 support httpfoundation request response http bridge phpfig

Link: http://symfony.com/blog/psr-7-support-in-symfony-is-here

Fabien Potencier's Blog:
Create your own framework... on top of the Symfony2 Components (part 2)
Jan 05, 2012 @ 14:11:27

Fabien Potencier is back with the next installment of his "Building a framework on top of Symfony2" tutorial series with this look at using the HttpFoundation component to use the Request and Response classes to handle HTTP interaction. (Part one is here.)

The first step towards better code is probably to use an Object-Oriented approach; that's the main goal of the Symfony2 HttpFoundation component: replacing the default PHP global variables and functions by an Object-Oriented layer.

He shows how using this component not only makes OOP handling of requests/responses simpler, but also helps to make your application more secure through features already included in the HttpFoundation component. Sample code is included showing how to fetch the current request, get filtered values from the superglobals (GET/SERVER/etc) and how to respond with a refactored version of the "Hello world" message from the previous example.

tagged: symfony2 components framework custom tutorial series httpfoundation

Link:


Trending Topics: