News Feed
Sections




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

Rob Allen:
Returning JSON errors in a ZF2 application
September 09, 2013 @ 10:49:30

Rob Allen has a quick post to his site for the Zend Framework 2 users out there showing how to return JSON errors from your requests.

If you have a standard ZF2 application and accept application/json requests in addition to application/html, then you have probably noticed that when an error happens, HTML is created, even though the client has requested JSON. One way to fix this is to create a listener on MVC's render event to detect that an error has occurred and substitute a JsonModel in place of the ViewModel. The easiest way to do this in your ApplicationModule.

He includes some example code showing how to attach a listener for rendering ("onRenderError") and the code that defines the renderer itself. It goes through a series of checks on the request and then gets the error information from the Response. Some common errors are caught and replaced with a default message, otherwise its just directly sent to render. He then attaches the whole thing in the bootstrap and shows a simple way to test the output with a cURL call.

0 comments voice your opinion now!
return json zendframework2 application tutorial render

Link: http://akrabat.com/zend-framework-2/returning-json-errors-in-a-zf2-application/

blog comments powered by Disqus

Similar Posts

ParanoidFerret.com: Using Flex, PHP, and JSON to Modify a MySQL Database

MaltBlue.com: \Zend\Db\Sql - Creating Joins and Unions with Ease

Gonzalo Ayuso's Blog: Protect files within public folders with mod_rewrite and PHP

SitePoint PHP Blog: Social Network Style Posting with PHP, MongoDB and jQuery - part 2

Sasa Stamenkovic's Blog: Building CLI Apps With Symfony Console Component


Community Events





Don't see your event here?
Let us know!


symfony2 community threedevsandamaybe interview laravel list code refactor testing unittest install series framework language release podcast developer api introduction opinion

All content copyright, 2014 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework