Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use psr-7 for laravel requests / responses?

i have been using slim 3 and finally got my head around psr-7. Now working with laravel i see that out of the box, psr-7 is not supported.

Now... is there a strong reason to follow psr-7 or the laravel request styles?

personal preference for example, is not a strong reason.

I just don't want to code entire apps with laravel request classes, that is Illuminate\Http\Request just to find out in a year that I really should have followed the psr-7 standard.

like image 395
Toskan Avatar asked Aug 15 '16 23:08

Toskan


1 Answers

Short Answer: No

Long Answer:

Consider the purpose of PSR (PHP Standard Recommendations) and how much it means to you that every facet of your chosen framework follows these standards.

Laravel doesn't come with PSR-7 supported out of the box, because the Symfony Component on which its Request/Response system is built is not PSR-7 compliant either. In fact, this component is often found at the heart of MANY frameworks and was written at a time before the idea of PSR caught on.

In order for Laravel - or any other frameworks that depend on this component - to become PSR-7 compliant, it will have to change its dependency on the Symfony HTTP Foundation component to something else, or perhaps even roll its own implementation.

Remember, ALL PSR recommendations are just that: Recommendations.

None of them are required.

Their sole purpose is to help unify the writing of PHP into more predictable formats so that all PHP devs who are at least familiar with those standards will be able to then follow and more easily understand each codebase that complies.

There is no compelling reason to refactor any codebase or framework of reasonable complexity to be PSR-compliant just for the sake of it

As you yourself said, personal preference is not a strong enough reason to overhaul a codebase.

As for your use-case, you need to ask yourself what a PSR-7 compliant framework will achieve for you over a non-compliant one. The issue isn't one of maintainability, as PSR-compliant code is no more - or no less - maintainable than non-compliant code.

If you're writing an API in which you'll need total control over the Request/Response lifecycle, then perhaps a PSR-7 compliant framework like Slim would be best to use. If you're writing a general-purpose application in which the only requests and responses you'll concern yourself with are a handful of AJAX and JSON routes, then you're probably fine with Laravel or Lumen.

Don't get caught up too much in whether or not a given framework complies with the FIG. Just do your research, and make sure that the toolset you choose best suits your needs.

like image 114
maiorano84 Avatar answered Oct 20 '22 12:10

maiorano84