HEAD在 apigility 中实现请求时,我无法以未找到的 404 响应。默认情况下, ApigilityHEAD使用与请求相同的资源方法处理请求GET。这就是fetch($id)方法。
我的资源如下所示:
class MyResource extends AbstractResourceListener implements ResourceInterface
{
public function fetch($id)
{
$entity = $this->getEntityById($id, false);
if ($entity === null) {
return new ApiProblemResponse(new ApiProblem(404, 'Entity with ID ' . $id . ' not found'));
}
return $entity;
}
}
当使用 REST 客户端访问资源时,HEAD我得到 200 OK。我可以确认ApiProblemResponse是从方法返回的。使用GETI 请求资源,我得到了预期的 404 响应。
似乎HEAD与. GET_ AbstractRestfulController以下是相关代码的代码片段:
case 'get':
$id = $this->getIdentifier($routeMatch, $request);
if ($id !== false) {
$action = 'get';
$return = $this->get($id);
break;
}
$action = 'getList';
$return = $this->getList();
break;
// HEAD
case 'head':
$id = $this->getIdentifier($routeMatch, $request);
if ($id === false) {
$id = null;
}
$action = 'head';
$this->head($id);
$response = $e->getResponse();
$response->setContent('');
$return = $response;
break;
在 get 实现中,resourceListener 方法的结果作为应用程序响应返回。这对我来说似乎是正确的。在头部实现中使用了 MvcEvent 响应,因此忽略了ApiProblemResponse从 resourceListener 返回的 I。我无法MvcEvent从我的ResourceListener.
有没有人能够为HEAD请求返回 404 的解决方案?