404 HTTP errors that do not appear in JSON using Zend framework 2

I am creating a simple soothing api using zend framework2 and I have links to Rob Allen's notes on this subject, as well as this excellent tutorial from http://hounddog.github.com/blog/getting-started-with- rest-and -Send-frame 2 /

below is my mod_config.php. You will see that I have routes and the JSON viewing strategy is configured. I understand that when setting up a JSON strategy in this way, it supports all modules. The problem is that if you enter the wrong route, the 404 response is sent back to html, even if the Accept header asks for Application / json.

I struggled with this for 2 days, whenever we advised or helped

This curl call for api generates the expected 404 error.

curl -i -H "Accept: application/json" http://myapi-dev.local/xxx/1 

Module_config.php

 return array( 'controllers' => array( 'invokables' => array( 'Dips\Controller\Roles' => 'Dips\Controller\RolesController', //maps controller alias to a physical controller ), ), 'router' => array( 'routes' => array( 'dips' => array( 'type' => 'segment', 'options' => array( 'route' => '/dips/roles/:apikey/:uname/:appname', 'constraints' => array( 'apikey' => '[a-zA-Z0-9]+', 'uname' => '[a-zA-Z]+', 'appname' => '[a-zA-Z][a-zA-Z0-9_-]*', ), 'defaults' => array( 'controller' => 'Dips/Controller/Roles', ), ), ), ), ), 'view_manager' => array( 'strategies' => array( 'ViewJsonStrategy', ), ), ); 
+4
source share
1 answer

You will need to implement an additional rendering strategy and put it on the stack above the standard 404 rendering strategy. In addition, you can extend the existing ViewJsonStrategy to enable error handling.

The default 404 strategy is Zend / Mvc / View / Http / RouteNotFoundStrategy. If you look at the detectNotFoundError method, you can see when it starts.

0
source

All Articles