dongmaqiu6084 2013-03-05 09:29
浏览 39
已采纳

symfony 1.4中的Json 404

I am building a json api. Formats are managed nicely via the routing.yml configuration file:

user_info:
  url: /users/:user_id
  class: sfRequestRoute
  param: { module: user, action: details, sf_format: json }
  requirements:
    user_id: \d+
    sf_method: [GET]

Now I would like to make sure that any request that is sent to my app will return a result in json format (example: "/dummy" should return an error in json). I am getting on track with the following default route at the end of the same file:

default:
  url: /*
  param: { sf_format: json }

When I route to an inexisting page or module, this renders correctly a json... Error! The html page generated was a 404, now with this routing I get a 500 (server error). How can I keep a similar behaviour as the one I had in the default html page, just changing the rendering into a json output?

EDIT - following 1Ed instructions

So I edited my factories.yml, created a default module in my api project, there in my default actions added the executeError404 function mentioned below... Then I Set up the corresponding Error404Success.php page...

For some reason, I still need the routes, which I modified to avoid the :module and :action keywords:

default_index:
  url:   /:a

default:
  url: /:a/:b/*

And here there I am ! My 404 page comes in json format for the whole api :) Thanks a lot !

  • 写回答

1条回答 默认 最新

  • douzong5835 2013-03-05 11:40
    关注

    This default route will execute the index action of the default module (by default). You should add a indexSuccess.json.php template file to the default module or modify the default/index action to return a json response early.

    If you just want to return a 404 every time if this default route matches you can change the default action to error404 in factories.yml:

    all:
      routing:
        class: sfPatternRouting
        param:
          generate_shortest_url:            true
          extra_parameters_as_query_string: true
          default_action:                   error404
    

    Or you can set the request format of the default 404 action explicitly to json like this

    public function executeError404(sfWebRequest $request)
    {
      $request->setRequestFormat('json');
    }
    

    and you can remove the default route.

    I recommend NOT to use these kind of default routes as they make really hard to keep track of all possible URL-s to the site. You should create a separate route for each particular action or use route collections.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛