在HEAD请求中无法响应404

时间:2014-12-11 09:32:04

标签: php zend-framework2 apigility

在apigility中实施HEAD请求时,我无法回复404未找到的回复。 Apigility默认使用与HEAD请求相同的资源方法处理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;
    }
}

使用HEAD使用REST客户端访问资源时,我得到200 OK。我可以确认从方法返回ApiProblemResponse。 使用GET请求资源我按预期获得404响应。

这似乎与HEADGETAbstractRestfulController的处理有关。以下是相关代码的代码段:

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方法的结果作为应用程序响应返回。这对我来说似乎是正确的。 在head实现中使用了MvcEvent响应,因此忽略了我从resourceListener返回的ApiProblemResponse。我无法从MvcEvent访问ResourceListener

是否有人有解决方案可以为HEAD请求返回404?

0 个答案:

没有答案
相关问题