Symfony2在生产环境中删除“if-modified-since”参数

4
我用Symfony2为iOS应用程序创建了一个API。该应用程序通过标题参数if-modified-since发送GET请求。

"If-Modified-Since" = "Thu, 07 Nov 2013 11:50:52 GMT";

在控制器中,我检查参数并在日期更新时返回数据。但是,在生产环境中,Symfony2中的参数将在类Symfony\Component\HttpKernel\HttpCache\HttpCache.php中被删除。下面是该类中的函数。
/**
 * Forwards the Request to the backend and determines whether the response should be stored.
 *
 * This methods is triggered when the cache missed or a reload is required.
 *
 * @param Request $request A Request instance
 * @param Boolean $catch   whether to process exceptions
 *
 * @return Response A Response instance
 */
protected function fetch(Request $request, $catch = false)
{
    $subRequest = clone $request;

    // send no head requests because we want content
    $subRequest->setMethod('GET');

    // avoid that the backend sends no content
    $subRequest->headers->remove('if_modified_since');
    $subRequest->headers->remove('if_none_match');

    $response = $this->forward($subRequest, $catch);

    if ($this->isPrivateRequest($request) && !$response->headers->hasCacheControlDirective('public')) {
        $response->setPrivate(true);
    } elseif ($this->options['default_ttl'] > 0 && null === $response->getTtl() && !$response->headers->getCacheControlDirective('must-revalidate')) {
        $response->setTtl($this->options['default_ttl']);
    }

    if ($response->isCacheable()) {
        $this->store($request, $response);
    }

    return $response;
}

看起来Symfony找不到此请求的响应的缓存条目,并删除此参数以确保有内容可以缓存,但它从未找到条目。即使经过多次重新加载,仍然是这样。

我需要在控制器中使用此参数,目前应用程序没有机会更改参数或发送具有日期值的其他参数。

我是否需要进行一些配置或其他操作?

如果有人能帮助我,我将非常感激。


1
如果您发送了 If-Modified-Since,而Symfony删除了 if_modified_since,那么问题是什么。Symfony使用此 if_modified_since 用于其内部缓存验证目的。它从不删除 If-Modified-Since 标头。 - xiidea
你确实需要配置http缓存。也许你忘记在web/app.php包装内核了? - Jasper N. Brouwer
1个回答

1
在您的生产环境中,似乎使用的是app/AppCache.php而不是app/AppKernel.php。因此,如果您想自己处理它,请切换回app/AppKernel.php在您的web/app.php中。
require_once __DIR__.'/../app/AppKernel.php';
//require_once __DIR__.'/../app/AppCache.php';

$kernel = new AppKernel('prod', false);
$kernel->loadClassCache();
//$kernel = new AppCache($kernel);

详细了解Symfony http cache


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接