从 Zend Framework 2 中的 404 JSON 响应中删除其他消息
Remove additional messages from 404 JSON response in Zend Framework 2
我正在 ZF2 returning JSON 中为每条路线创建一个 RESTful API 响应。对于其中一条路线,我需要 return 一个 404 响应,但是 return $this->notFoundAction();
是不可接受的,因为它 return 是一个视图而不是 JSON。因此,我正在执行以下操作:
$response = $this->getResponse();
$response->setStatusCode(404);
return new JsonModel(array(
'error_message' => "Client $clientRef doesn't exist"
));
对此进行测试,我得到了带有 404 状态代码的响应,并且显示了我的错误消息。但是,响应中包含其他 Zend 生成的消息:
{
"error_message": "Client test-client doesn't exist",
"message": "Page not found.",
"reason": "error-controller-cannot-dispatch",
"display_exceptions": true,
"controller": "Timesheet\Controller\TimesheetRest",
"controller_class": null
}
如果我在配置中将 display_not_found_reason
和 display_exceptions
都更改为 false
,那么它将删除一些额外的细节,但 message
仍然存在:
{
"error_message": "Client test-client doesn't exist",
"message": "Page not found."
}
如何从响应正文中删除所有额外的详细信息?
请注意,我只有在 return 进入 404 状态时才会收到这些消息。当 returning 400、403 或 409 时,只有我的错误消息被 returned,没有任何其他消息。
不确定是否相关,但相关模块的配置文件如下(注意我有 2 个控制器,一个用于前端,一个用于 API):
<?php
return array(
'controllers' => array(
'invokables' => array(
'Timesheet\Controller\Timesheet' => 'Timesheet\Controller\TimesheetController',
'Timesheet\Controller\TimesheetRest' => 'Timesheet\Controller\TimesheetRestController'
)
),
'router' => array(
'routes' => array(
'timesheet' => array(
'type' => 'Segment',
'options' => array(
// Change this to something specific to your module
'route' => '/timesheet[/:action][/:id]',
'defaults' => array(
// Change this value to reflect the namespace in which
// the controllers for your module are found
'__NAMESPACE__' => 'Timesheet\Controller',
'controller' => 'Timesheet',
'action' => 'index'
),
'constraints' => array(
'action' => 'index|pending|review|save|submit|unauthorised',
'id' => '[1-9][0-9]*'
)
)
),
'timesheet-rest' => array(
'type' => 'Segment',
'options' => array(
// Change this to something specific to your module
'route' => '/api/timesheet[/:id]',
'defaults' => array(
// Change this value to reflect the namespace in which
// the controllers for your module are found
'__NAMESPACE__' => 'Timesheet\Controller',
'controller' => 'TimesheetRest',
),
'constraints' => array(
'id' => '[1-9][0-9]*'
)
)
),
)
),
'view_manager' => array(
'strategies' => array(
'ViewJsonStrategy'
),
'template_path_stack' => array(
'Timesheet' => __DIR__ . '/../view'
)
)
);
显示空白message
在查看 Zend/Mvc/View/Http/RouteNotFoundStrategy
之后,有一个函数 prepareNotFoundViewModel
可以将 message
添加到响应中。但是,这仅在尚未定义 message
时发生。将我的代码更改为:
$response = $this->getResponse();
$response->setStatusCode(404);
return new JsonModel(array(
'message' => '',
'error_message' => "Client $clientRef doesn't exist"
));
然后会给我一个响应正文:
{
"message": "",
"error_message": "Client test-client doesn't exist"
}
这至少消除了两条消息之间的混淆。
删除message
但是,直接将代码更改为 return 响应,而不是使用 JsonModel
,完全阻止 message
被注入。以下代码:
$response = $this->getResponse();
$response->setStatusCode(404);
$response->setContent(json_encode(array(
'error_message' => "Client $clientRef doesn't exist"
)));
return $response;
将此作为回应:
{
"error_message":"Client test-client doesn't exist"
}
我正在 ZF2 returning JSON 中为每条路线创建一个 RESTful API 响应。对于其中一条路线,我需要 return 一个 404 响应,但是 return $this->notFoundAction();
是不可接受的,因为它 return 是一个视图而不是 JSON。因此,我正在执行以下操作:
$response = $this->getResponse();
$response->setStatusCode(404);
return new JsonModel(array(
'error_message' => "Client $clientRef doesn't exist"
));
对此进行测试,我得到了带有 404 状态代码的响应,并且显示了我的错误消息。但是,响应中包含其他 Zend 生成的消息:
{
"error_message": "Client test-client doesn't exist",
"message": "Page not found.",
"reason": "error-controller-cannot-dispatch",
"display_exceptions": true,
"controller": "Timesheet\Controller\TimesheetRest",
"controller_class": null
}
如果我在配置中将 display_not_found_reason
和 display_exceptions
都更改为 false
,那么它将删除一些额外的细节,但 message
仍然存在:
{
"error_message": "Client test-client doesn't exist",
"message": "Page not found."
}
如何从响应正文中删除所有额外的详细信息?
请注意,我只有在 return 进入 404 状态时才会收到这些消息。当 returning 400、403 或 409 时,只有我的错误消息被 returned,没有任何其他消息。
不确定是否相关,但相关模块的配置文件如下(注意我有 2 个控制器,一个用于前端,一个用于 API):
<?php
return array(
'controllers' => array(
'invokables' => array(
'Timesheet\Controller\Timesheet' => 'Timesheet\Controller\TimesheetController',
'Timesheet\Controller\TimesheetRest' => 'Timesheet\Controller\TimesheetRestController'
)
),
'router' => array(
'routes' => array(
'timesheet' => array(
'type' => 'Segment',
'options' => array(
// Change this to something specific to your module
'route' => '/timesheet[/:action][/:id]',
'defaults' => array(
// Change this value to reflect the namespace in which
// the controllers for your module are found
'__NAMESPACE__' => 'Timesheet\Controller',
'controller' => 'Timesheet',
'action' => 'index'
),
'constraints' => array(
'action' => 'index|pending|review|save|submit|unauthorised',
'id' => '[1-9][0-9]*'
)
)
),
'timesheet-rest' => array(
'type' => 'Segment',
'options' => array(
// Change this to something specific to your module
'route' => '/api/timesheet[/:id]',
'defaults' => array(
// Change this value to reflect the namespace in which
// the controllers for your module are found
'__NAMESPACE__' => 'Timesheet\Controller',
'controller' => 'TimesheetRest',
),
'constraints' => array(
'id' => '[1-9][0-9]*'
)
)
),
)
),
'view_manager' => array(
'strategies' => array(
'ViewJsonStrategy'
),
'template_path_stack' => array(
'Timesheet' => __DIR__ . '/../view'
)
)
);
显示空白message
在查看 Zend/Mvc/View/Http/RouteNotFoundStrategy
之后,有一个函数 prepareNotFoundViewModel
可以将 message
添加到响应中。但是,这仅在尚未定义 message
时发生。将我的代码更改为:
$response = $this->getResponse();
$response->setStatusCode(404);
return new JsonModel(array(
'message' => '',
'error_message' => "Client $clientRef doesn't exist"
));
然后会给我一个响应正文:
{
"message": "",
"error_message": "Client test-client doesn't exist"
}
这至少消除了两条消息之间的混淆。
删除message
但是,直接将代码更改为 return 响应,而不是使用 JsonModel
,完全阻止 message
被注入。以下代码:
$response = $this->getResponse();
$response->setStatusCode(404);
$response->setContent(json_encode(array(
'error_message' => "Client $clientRef doesn't exist"
)));
return $response;
将此作为回应:
{
"error_message":"Client test-client doesn't exist"
}