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

Chris Fidao:
Laravel and Content Negotiation
Jan 18, 2016 @ 17:57:35

Chris Fidao has a quick post to his site looking at content negotiation in Laravel apps using some of the framework's own built-in functionality.

Here's a little bit about content negotiation. An HTTP client, such as your browser, or perhaps jQuery's ajax method, can set an Accept header as part of an HTTP request. [...] This header is meant to tell the server what content types it is willing to accept.

He starts with a bit of illustration as to what the Accept header is and what kinds of values it supports (and how it looks as a HTTP header). He then shows how to check the Accept header value inside the current request. He also shows the "shortcut" Laravel provides to test if the Accept header specifically references JSON with the wantsJson method. He also mentions the accepts and prefers methods for checks that need to be a bit more in-depth.

tagged: content type accepts negotiation wantsjson prefers

Link: http://fideloper.com/laravel-content-negotiation

Paul Jones:
Action-Domain-Responder, Content Negotiation, and Routers
Jul 18, 2014 @ 15:17:57

In his latest post Paul Jones comes back to his proposed application structure, the idea of Action-Domain-Responder, and answers some questions about where content negotiation happens and routing.

While talking about Action-Domain-Responder on the Crafting Code Tour, one of the common questions I got was: “Where does content negotiation happen?” My response was always: “Where does it happen in Model-View-Controller?” That opened up a discussion on how content negotiation is a tricky bit that can go in different places, depending on how you want the concerns separated, and is not a problem specific to ADR.

He goes on and tries to answer the question a bit better, pointing out that "it's a problem for everyone" isn't really good enough to take action on. He works through the different pieces of the ADR pattern, trying to reason out where the right fit is. He suggests a "first filter" on the Controller level, more specifically at the Router level. That's not to say that the Router needs to know about content handling, but it does need to know how to pass that information on.

tagged: action domain responder content negotiation routing

Link: http://paul-m-jones.com/archives/6020


Trending Topics: