dongyu3967 2016-11-25 11:47
浏览 78

Symfony2:在RedirectResponse中设置Last-Modified

here is what I have:

I have got a route where I call a service to rotate a given image.

/**
 * @Route("/media/rotate/{id}", name="rotate_image")
 *
 * @ParamConverter(name="media", class="RA\MediaBundle\Entity\Media")
 *
 * @param Media $media
 *
 * @return RedirectResponse
 */
public function rotateImage(Media $media)
{
    $this->get('ra.image.rotator.service')->rotateImage($media, 90);

    // do the redirect
}

and here is what I would like to do:

Do a redirect on the route I came from (with the help of referer). This does work, but after the redirect the rotated image isn't displayed, because it is cached.

So I thought setting "Last-Modified" in the header to the current DateTime would force the browser to reload the image. I tried to do it the following way:

    if ($referer = $request->headers->get('referer')) {
        $response = new RedirectResponse($referer);
        $response->setLastModified(new \DateTime());

        return $response;
    }

But unfortunately "Last-Modified" is only set in the header of the rotate_image route, not in the one I got redirected to.

Any idea how to solve this issue?

Edit:

I have "solved" my issue by just adding a version parameter to the media urls like this:

return $mediaUrl . '?v=' . md5($media->getUpdatedAt()->format('Y-m-d H:i:s'));

So after rotating the image I'm updating the media entity so that the version will change and the "new" image will be shown.

  • 写回答

1条回答 默认 最新

  • dpmir1988 2016-11-25 21:47
    关注

    Put the logic of the image list in a controller and create sub-requests with twig function "render" like: {{ render(controller('AppBundle:ImageController:index', { 'media': media })) }}

    You can create an event listener for "kernel.response". take a look at symfony.com/doc/current/event_dispatcher.html Modify the headers of the response with it. Maybe check last modified timestamp of your image.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化