Marks the response stale by setting the Age header to be equal to the maximum age of the response.
public expire ( ) : |
||
Résultat |
/** * Renders temporary file * * @return Response */ public function renderTemporaryAction() { $name = $this->getRequest()->get('name'); $fileManager = $this->container->get('thrace_media.filemanager'); $content = $fileManager->getTemporaryFileBlobByName($name); $response = new Response($content); $response->headers->set('Accept-Ranges', 'bytes'); $response->headers->set('Content-Length', mb_strlen($content)); $response->headers->set('Content-Type', $this->getMimeType($content)); $response->expire(); return $response; }
public function showPageAction($id, $extraParams = null, $currentpage = null, $totalpageitems = null, $linkUrlParams = null) { // Get data to display $page = $this->getDoctrine()->getRepository('BlogBundle:Blog')->find($id); $userRole = $this->get('sonata_user.services.helpers')->getLoggedUserHighestRole(); $settings = $this->get('bardiscms_settings.load_settings')->loadSettings(); // Simple ACL for publishing if ($page->getPublishState() == 0) { return $this->render404Page(); } if ($page->getPublishState() == 2 && $userRole == '') { return $this->render404Page(); } if ($userRole == "") { $publishStates = array(1); } else { $publishStates = array(1, 2); } if ($this->container->getParameter('kernel.environment') == 'prod' && $settings->getActivateHttpCache()) { $response = new Response(); // set a custom Cache-Control directive $response->headers->addCacheControlDirective('must-revalidate', true); // set multiple vary headers $response->setVary(array('Accept-Encoding', 'User-Agent')); // create a Response with a Last-Modified header $response->setLastModified($page->getDateLastModified()); // Set response as public. Otherwise it will be private by default. $response->setPublic(); //var_dump($response->isNotModified($this->getRequest())); //var_dump($response->getStatusCode()); if (!$response->isNotModified($this->getRequest())) { // Marks the Response stale $response->expire(); } else { // return the 304 Response immediately $response->setSharedMaxAge(3600); return $response; } } // Set the website settings and metatags $page = $this->get('bardiscms_settings.set_page_settings')->setPageSettings($page); // Set the pagination variables if (!$totalpageitems) { if (is_object($settings)) { $totalpageitems = $settings->getBlogItemsPerPage(); } else { $totalpageitems = 10; } } // Render the correct view depending on pagetype return $this->renderPage($page, $id, $publishStates, $extraParams, $currentpage, $totalpageitems, $linkUrlParams); }
public function testExpire() { $response = new Response(); $response->headers->set('Cache-Control', 'max-age=100'); $response->expire(); $this->assertEquals(100, $response->headers->get('Age'), '->expire() sets the Age to max-age when present'); $response = new Response(); $response->headers->set('Cache-Control', 'max-age=100, s-maxage=500'); $response->expire(); $this->assertEquals(500, $response->headers->get('Age'), '->expire() sets the Age to s-maxage when both max-age and s-maxage are present'); $response = new Response(); $response->headers->set('Cache-Control', 'max-age=5, s-maxage=500'); $response->headers->set('Age', '1000'); $response->expire(); $this->assertEquals(1000, $response->headers->get('Age'), '->expire() does nothing when the response is already stale/expired'); $response = new Response(); $response->expire(); $this->assertFalse($response->headers->has('Age'), '->expire() does nothing when the response does not include freshness information'); }
/** * Set the no-cache headers for all responses, if requested. * * <b>NOTE</b> - This header will be overridden automatically if a * <samp>RequestDispatcher->doForward</samp> call is ultimately * invoked. * * @param \Symfony\Component\HttpFoundation\Request $request The kernel request we are * processing * @param \Symfony\Component\HttpFoundation\Response $response The kernel response we are * creating */ protected function processNoCache(\Symfony\Component\HttpFoundation\Request $request, \Symfony\Component\HttpFoundation\Response $response) { if ($this->moduleConfig->getControllerConfig()->getNocache()) { $response->headers->set('Pragma', 'No-cache'); $response->headers->set('Cache-Control', 'no-cache'); $response->expire(); } }
/** * Renders temporary image * * @param string $name * @return Response */ public function renderThumbnailTemporaryAction() { $name = $this->getRequest()->get('name'); $filter = $this->getRequest()->get('filter'); $imageManager = $this->container->get('thrace_media.imagemanager'); $filterManager = $this->container->get('liip_imagine.filter.manager'); try { $content = $filterManager->applyFilter($imageManager->loadTemporaryImageByName($name), $filter); } catch (FileNotFound $e) { throw new NotFoundHttpException(); } $response = new Response($content); $response->headers->set('Accept-Ranges', 'bytes'); $response->headers->set('Content-Length', mb_strlen($content)); $response->headers->set('Content-Type', $this->getMimeType($content)); $response->expire(); return $response; }
/** * gets the export Response * * @return Response */ public function getResponse() { // Response $kernelCharset = $this->container->getParameter('kernel.charset'); if ($this->charset != $kernelCharset && function_exists('mb_strlen')) { $this->content = mb_convert_encoding($this->content, $this->charset, $kernelCharset); $filesize = mb_strlen($this->content, '8bit'); } else { $filesize = strlen($this->content); $this->charset = $kernelCharset; } $headers = array('Content-Description' => 'File Transfer', 'Content-Type' => $this->getMimeType(), 'Content-Disposition' => sprintf('attachment; filename="%s"', $this->getBaseName()), 'Content-Transfer-Encoding' => 'binary', 'Cache-Control' => 'must-revalidate', 'Pragma' => 'public', 'Content-Length' => $filesize); $response = new Response($this->content, 200, $headers); $response->setCharset($this->charset); $response->expire(); return $response; }
/** * @Route("") * @Theme("admin") * * view items * * @param Request $request * * @return Response HTML output */ public function indexAction(Request $request) { // Get parameters $startnum = $request->query->get('startnum', 1); $orderBy = $request->query->get('orderby', 'pageid'); $currentSortDirection = $request->query->get('sdir', Column::DIRECTION_DESCENDING); $filterForm = $this->createForm(new FilterType(), $request->query->all(), array('action' => $this->generateUrl('zikulapagesmodule_admin_index'), 'method' => 'POST', 'entityCategoryRegistries' => CategoryRegistryUtil::getRegisteredModuleCategories($this->name, 'PageEntity', 'id'))); $filterForm->handleRequest($request); $filterData = $filterForm->isSubmitted() ? $filterForm->getData() : $request->query->all(); $sortableColumns = new SortableColumns($this->get('router'), 'zikulapagesmodule_admin_index', 'orderby', 'sdir'); $sortableColumns->addColumn(new Column('pageid')); // first added is automatically the default $sortableColumns->addColumn(new Column('title')); $sortableColumns->addColumn(new Column('cr_date')); $sortableColumns->setOrderBy($sortableColumns->getColumn($orderBy), $currentSortDirection); $sortableColumns->setAdditionalUrlParameters(array('language' => isset($filterData['language']) ? $filterData['language'] : null, 'filtercats_serialized' => isset($filterData['categories']) ? serialize($filterData['categories']) : null)); $pages = new PageCollectionManager($this->container->get('doctrine.entitymanager')); $pages->setStartNumber($startnum); $pages->setItemsPerPage(\ModUtil::getVar($this->name, 'itemsperpage')); $pages->setOrder($orderBy, $currentSortDirection); $pages->enablePager(); $pages->setFilterBy($filterData); $templateParameters = array(); $templateParameters['filter_active'] = isset($filterData['category']) && count($filterData['category']) > 0 || !empty($filterData['language']); $templateParameters['sort'] = $sortableColumns->generateSortableColumns(); $templateParameters['pages'] = $pages->get(); $templateParameters['lang'] = ZLanguage::getLanguageCode(); $templateParameters['pager'] = $pages->getPager(); $templateParameters['modvars'] = \ModUtil::getModvars(); // temporary solution $templateParameters['filterForm'] = $filterForm->createView(); // attempt to disable caching for this only $response = new Response(); $response->expire(); return $this->render('ZikulaPagesModule:Admin:view.html.twig', $templateParameters, $response); }