使用 Zend 框架 2 未在 JSON 中呈现 404 HTTP 错误 [英] 404 HTTP errors not being rendered in JSON using Zend framework 2

查看:22
本文介绍了使用 Zend 框架 2 未在 JSON 中呈现 404 HTTP 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 zend framework2 创建一个简单的 restful api,我参考了 Rob Allen 关于该主题的笔记以及 http://hounddog.github.com/blog/getting-started-with- 的这个优秀教程-rest-and-zend-framework-2/

I'm creating a simple restful api using zend framework2 and I've references Rob Allen's notes on the subject as well as this excellent tutorial by http://hounddog.github.com/blog/getting-started-with-rest-and-zend-framework-2/

下面是我的module_config.php.您会看到我配置了路由和 JSON 视图策略.我的理解是,当您以这种方式设置 JSON 策略时,它可以容纳所有模块.问题是,当输入无效路由时,即使 Accept 标头正在请求 Application/json,也会以 html 形式发回 404 响应.

below is my module_config.php. You'll see that I have routes and the JSON view strategy configured. My understanding is that when you set up the JSON strategy in this fashion it accommodates all modules. The issue is that when an invalid route is entered the 404 response is sent back in html even though the Accept header is requesting Application/json.

我已经为此苦苦挣扎了 2 天,现在任何建议或帮助将不胜感激

I've been struggling with this for a 2 days now any advice or help would be appreciated

这个对 api 的 curl 调用产生了预期的 404 错误.

This curl call to the api generates the expected 404 error.

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

Module_config.php

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',
      ),
    ),
);

推荐答案

您需要实施额外的渲染策略并将其放在默认 404 渲染策略上方的堆栈中.或者,您可以扩展现有的 ViewJsonStrategy 以包含错误处理.

You would need to implement an additional rendering strategy and put it in the stack above the default 404 rendering strategy. Alternatively, you could extend the existing ViewJsonStrategy to include error handling.

默认的 404 策略是 Zend/Mvc/View/Http/RouteNotFoundStrategy.如果您查看它的 detectNotFoundError 方法,您可以看到它何时被触发.

The default 404 Strategy is Zend/Mvc/View/Http/RouteNotFoundStrategy. If you look at it's detectNotFoundError method, you can see when it gets triggered.

这篇关于使用 Zend 框架 2 未在 JSON 中呈现 404 HTTP 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆