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 @ 11: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

Rob Allen's Blog:
Returning JSON using the Accept header in ZF2
Mar 30, 2012 @ 08:01:56

In a previous post Rob Allen showed how to return JSON data from a controller in a Zend Framework 2 application. In this new post he shows how to use the "Accepts" header from the client to do the same thing.

Following yesterday's article on returning JSON from a ZF2 controller action, Lukas suggested that I should also demonstrate how to use the Accept header to get JSON. So this is how you do it!

You'll need to create the JsonStrategy first, then you can return the ViewModel from the controller. If all goes well, you should see the sample HTML page rendered in a browser and JSON output when requested with the right "Accept" header (he uses curl in his example).

tagged: json return zendframework2 tutorial jsonstrategy accepts

Link: