Php prod环境中异常消息的FOSRestBundle配置

Php prod环境中异常消息的FOSRestBundle配置,php,symfony,fosrestbundle,Php,Symfony,Fosrestbundle,我正在努力解决与FOSRestBundle(版本0.13.*)相关的问题 我有一些RESTAPI抛出一些异常,我想没有什么异常。但是,尽管我做了特定的配置,允许在响应中格式化异常消息,即使在生产中也是如此(遵循我在这里找到的文档:),JSON响应仍然是空的 示例如下: http://host/app_dev.php/api/postcode/search?postcode= 结果: HTTP 400: {"status":"error","status_code":400,"status_

我正在努力解决与FOSRestBundle(版本0.13.*)相关的问题

我有一些RESTAPI抛出一些异常,我想没有什么异常。但是,尽管我做了特定的配置,允许在响应中格式化异常消息,即使在生产中也是如此(遵循我在这里找到的文档:),JSON响应仍然是空的

示例如下:

http://host/app_dev.php/api/postcode/search?postcode=  
结果:

HTTP 400: {"status":"error","status_code":400,"status_text":"Bad Request","current_content":"","message":"You must provide a postcode"}
HTTP 400: []
但是

结果:

HTTP 400: {"status":"error","status_code":400,"status_text":"Bad Request","current_content":"","message":"You must provide a postcode"}
HTTP 400: []
我的API控制器如下所示:

/**
 * Search post codes
 *
 * @param Request   $request   Request
 * @param Promotion $promotion Promotion
 *
 * @Rest\View()
 *
 * @throws BadRequestHttpException
 * @return array
 */
public function searchAction(Request $request, Promotion $promotion)
{
    // Get post code
    $postCode = $request->query->get('postcode');
    if (!$postCode) {
        throw new BadRequestHttpException('You must provide a postcode');
    }

    // SOME LOGIC HERE TO GET DATA

    return $data;
}
fos_rest:
    routing_loader:
        default_format: json
    view:
        mime_types:
            json: ['application/json; charset=UTF-8']
        formats:
            json: true
        view_response_listener: force
    format_listener: false
    access_denied_listener:
        json: true
    body_listener: true
    exception:
        messages:
            Symfony\Component\HttpKernel\Exception\BadRequestHttpException: true
fos_rest配置如下所示:

/**
 * Search post codes
 *
 * @param Request   $request   Request
 * @param Promotion $promotion Promotion
 *
 * @Rest\View()
 *
 * @throws BadRequestHttpException
 * @return array
 */
public function searchAction(Request $request, Promotion $promotion)
{
    // Get post code
    $postCode = $request->query->get('postcode');
    if (!$postCode) {
        throw new BadRequestHttpException('You must provide a postcode');
    }

    // SOME LOGIC HERE TO GET DATA

    return $data;
}
fos_rest:
    routing_loader:
        default_format: json
    view:
        mime_types:
            json: ['application/json; charset=UTF-8']
        formats:
            json: true
        view_response_listener: force
    format_listener: false
    access_denied_listener:
        json: true
    body_listener: true
    exception:
        messages:
            Symfony\Component\HttpKernel\Exception\BadRequestHttpException: true
据我所知,fos_rest.exception.messages配置数组应该列出我希望在生产中序列化错误消息的异常。正如您在控制器的代码中所看到的,此响应包含将显示给客户端的已翻译错误消息。为什么忽略此配置? 我可以肯定地说,即使在prod环境中,配置也已正确加载,因为如果我在conf中错误地说出类名,它将失败,并出现“无法加载类”异常


我错过了什么?提前感谢您给我的任何提示…

我还没有尝试过,但您似乎忘记了这句话:

 exception:
    codes:
      'Symfony\Component\Routing\Exception\ResourceNotFoundException': 404
试试看

你有没有提到:

twig:
   ...
   ...
   exception_controller: 'FOS\RestBundle\Controller\ExceptionController::showAction'

我有一个类似的问题,你的问题实际上帮我解决了! 我知道已经晚了,但我发现清除
prod
缓存有助于我解决这个问题

此外,以下是我的设置:

fos_rest:
    param_fetcher_listener: true
    body_listener:
        array_normalizer: fos_rest.normalizer.camel_keys
    format_listener: true
    view:
        view_response_listener: 'force'
        formats:
            json: true
        templating_formats:
            html: true
        force_redirects:
            html: true
        failed_validation: HTTP_BAD_REQUEST
        default_engine: twig
    routing_loader:
        default_format: json
    serializer:
        serialize_null: true
    access_denied_listener:
        json: true
    exception:
        enabled: true
        messages:
            Symfony\Component\HttpKernel\Exception\BadRequestHttpException: true
我还想知道,除非您使用HttpException,否则您是否必须始终将异常显式地添加到配置的
code
messages
部分:

throw new HttpException(400, "New comment is not valid.");

谢谢大家!<代码>例外。消息正是我要找的。您好,如果您将我的答案标记为正确与否,我将不胜感激:D谢谢