4

我用 Symfony2 为 iOS 应用程序制作了一个 API。应用程序发送带有标头参数的 GET 请求if-modified-since

"If-Modified-Since" = "星期四,2013 年 11 月 7 日 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 没有为请求的这个响应找到缓存条目,并删除了这个参数以确保会有内容要缓存,但他从来没有找到一个条目。也经过多次重装。

我在控制器中需要这个参数,目前应用程序不可能更改参数或发送带有日期值的其他参数。

我是否必须做一些配置或其他事情?

如果有人可以帮助我,我将不胜感激。

4

1 回答 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 缓存

于 2014-06-10T06:19:56.720 回答