This sets the status, removes the body, and discards any headers
that MUST NOT be included in 304 responses.
public setNotModified ( ) : |
||
return |
public function testSetNotModified() { $response = new Response(); $modified = $response->setNotModified(); $this->assertObjectHasAttribute('headers', $modified); $this->assertObjectHasAttribute('content', $modified); $this->assertObjectHasAttribute('version', $modified); $this->assertObjectHasAttribute('statusCode', $modified); $this->assertObjectHasAttribute('statusText', $modified); $this->assertObjectHasAttribute('charset', $modified); $this->assertEquals(304, $modified->getStatusCode()); }
/** * Action rendering the tree menu for admin interface. * Note that parameters are not used at all since the request is entirely forwarded to the legacy kernel. * * @param int $nodeId * @param int $modified * @param int $expiry * @param string $perm * * @return \Symfony\Component\HttpFoundation\Response */ public function viewMenu($nodeId, $modified, $expiry, $perm) { $response = new Response(); if ($this->getParameter('treemenu.http_cache')) { $request = $this->getRequest(); $response->setMaxAge($this->getParameter('treemenu.ttl_cache')); // Aggressive cache : Always return a 304 response if "If-Modified-Since" request header is present. if ($request->headers->has('If-Modified-Since')) { $response->setNotModified(); return $response; } } $result = $this->treeMenuKernel->run(); if ($result->hasAttribute('lastModified')) { $response->setLastModified($result->getAttribute('lastModified')); } $response->setContent($result->getContent()); return $response; }
/** * Generates a JavaScript file with client-side localization constants. * * @param Request $request Incoming request. * @return Response Prepared JavaScript file with client side localization * constants. */ public function indexAction(Request $request) { $locale = $request->attributes->get('locale'); $item = $this->getCache()->getItem('translation/js/' . $locale); $content = $item->get(Invalidation::OLD); if ($item->isMiss()) { $item->lock(); $messages = load_messages($locale); // Store JSON-encoded data to reduce count of json_encode calls. $content = sprintf('%s(%s);', 'Mibew.Localization.set', json_encode($messages)); $item->set($content); } // Session is started automatically during application initialization // and PHP sets "Cache-Control" and "Expires" headers to forbid caching // and to keep the session private. In this script we actually does not // use session stuff, thus we can remove these headers to provide // caching. Notice that all headers are removed to clear "Set-Cookie" // header with session ID and may be some other unsafe headers that // must not be cached. header_remove(); // The whole response body (JSON-encoded with a callback function) is // cached via cache backend, thus it's simpler to use Symfony's // Response class instead of JsonResponse. $response = new Response(); $response->headers->set('Content-Type', 'text/javascript'); // Set various cache headers $response->setPublic(); $response->setMaxAge(120); if ($item->getCreation()) { // Creation field can be unavailable for some cache drivers. $response->setLastModified($item->getCreation()); } $response->setETag(sha1($content)); if ($response->isNotModified($request)) { $response->setNotModified(); // We does not need to send content for the client. Just return 304 // status code. return $response; } // Pass the whole response for the client. $response->setContent($content); return $response; }
/** * Generate the HTTP response * 200 : a full body containing the stream * 304 : Not modified. * * @param array $options * @param $format * @param string $source * * @return Response * * @throws \Exception */ protected function createStreamResponse(array $options, $format, $source = self::DEFAULT_SOURCE) { $content = $this->getContent($options, $source); if ($this->mustForceRefresh() || $content->getLastModified() > $this->getModifiedSince()) { $response = new Response($this->getStringOutput($content, $format)); $response->headers->set('Content-Type', 'application/xhtml+xml'); if (!$this->container->getParameter('debril_rss_atom.private_feeds')) { $response->setPublic(); } $response->setMaxAge(3600); $response->setLastModified($content->getLastModified()); } else { $response = new Response(); $response->setNotModified(); } return $response; }
/** * Marks the response as not modified as per the Symfony */ public function setNotModified() { parent::setNotModified(); $this->setModel(null); return $this; }
$StatTracker->get("/resources/{resource_dir}/{resource}", function (Request $request, $resource) use($StatTracker) { switch ($resource) { case "style.css": $file = "./resources/css/style.less"; $lastModified = filemtime($file); $css = new Symfony\Component\HttpFoundation\Response("", 200, array("Content-Type" => "text/css")); $css->setLastModified(new \DateTime("@" . filemtime($file))); if ($css->isNotModified($request)) { $css->setNotModified(); } else { $parser = new Less_Parser(array("compress" => true)); $parser->parseFile($file, $request->getBaseUrl()); $css->setLastModified(new \DateTime("@" . filemtime($file))); $css->setContent($parser->getCss()); } return $css; break; case "stat-tracker.js": $js = new Symfony\Component\HttpFoundation\Response(); if ($js->isNotModified($request)) { $js->setNotModified(); } else { $content = $StatTracker['twig']->render("stat-tracker.js.twig"); $js->headers->set("Content-Type", "application/javascript"); $js->setContent($content); } return $js; break; } }); $StatTracker->run();
/** * setHeadersIfNotProcessed * * @param Response $response * @param Image $image * * @access protected * @return mixed */ protected function setHeadersIfNotProcessed(Response $response, \DateTime $lastMod) { $response->setNotModified(); $response->setLastModified($lastMod); }