/** * Returns the rendered subtree of each top-level toolbar link. * * @return \Symfony\Component\HttpFoundation\JsonResponse */ public function subtreesJsonp() { $subtrees = toolbar_get_rendered_subtrees(); $response = new JsonResponse($subtrees); $response->setCallback('Drupal.toolbar.setSubtrees.resolve'); // The Expires HTTP header is the heart of the client-side HTTP caching. The // additional server-side page cache only takes effect when the client // accesses the callback URL again (e.g., after clearing the browser cache // or when force-reloading a Drupal page). $max_age = 365 * 24 * 60 * 60; $response->setPrivate(); $response->setMaxAge($max_age); $expires = new \DateTime(); $expires->setTimestamp(REQUEST_TIME + $max_age); $response->setExpires($expires); return $response; }
/** * * @Route("/{_locale}/{_code}/statistics/{dataset}.json", requirements={"_locale" = "de|en|es|fr", "_code" = "[a-zA-Z0-9]{10}", "dataset" = "[a-zA-Z0-9_-]+"}) */ public function statisticsJsonAction($_code, $dataset, Request $request) { try { $context = $this->getContext($_code, 'statistics'); $stats = new Statistics($this, $this->account); if (preg_match('!^wallet-([0-9]+)$!', $dataset, $m)) { $method = 'getDatasetWallet'; if (method_exists($stats, $method)) { $data = $stats->{$method}($m[1]); if (!empty($data)) { $response = new JsonResponse($data); } } } else { $method = 'getDataset' . $dataset; if (method_exists($stats, $method)) { $response = new JsonResponse($stats->{$method}()); } } if (isset($response)) { $response->setMaxAge(900); $response->setExpires(new \DateTime('@' . (time() + 900))); $response->setPublic(); return $response; } } catch (\Exception $ex) { return new JsonResponse(['error' => $ex->getMessage()]); } }
/** * @param Request $request * @param mixed $data * * @return JsonResponse */ protected function getJsonResponse(Request $request, $data = null) { $date = new \DateTime(); $date->modify('+1 day'); $response = new JsonResponse($data); $response->setExpires($date); $response->setETag(md5($response->getContent())); $response->setPublic(); $response->isNotModified($request); $response->headers->set('X-Proudly-Crafted-By', "LesPolypodes.com"); // It's nerdy, I know that. return $response; }
/** * @Route("/create", name="api_create") */ public function createAction(Request $request) { if ($this->container->getParameter('secret') != $request->query->get('secret')) { return new JsonResponse(['error' => 'Invalid Secret']); } // Create Short Code $url = $request->query->get('url'); $code = $request->query->get('code', null); $shortUrl = $this->get('short_url')->create($url, $code); $response = new JsonResponse(['short_url' => $this->get('short_url')->shortUrl($shortUrl)]); $response->setPublic(); $response->setExpires(new DateTime('+1 day')); return $response; }
public function getListAction(Application $app) { $userList = $this->repository->findAll(); $date = new \DateTime(); $date->modify('+' . self::MAX_AGE . ' seconds'); $response = new JsonResponse($userList, JsonResponse::HTTP_OK); $responseHash = sha1($response->getContent()); $response->setMaxAge(self::MAX_AGE); $response->setSharedMaxAge(self::MAX_AGE); $response->setExpires($date); $response->setETag($responseHash); $response->isNotModified($app['request']); return $response; }
/** * @Route("/api/data", name="public_edk_registration_data", defaults={"_localeFromQuery" = true}) */ public function registrationsAction(Request $request) { try { $repository = $this->get('wio.edk.repo.published_data'); $registrations = $repository->getOpenRegistrations($this->project, $this->getProjectSettings()->get(EdkSettings::PUBLISHED_AREA_STATUS)->getValue()); $response = new JsonResponse($registrations); $response->setDate(new DateTime()); $exp = new DateTime(); $exp->add(new DateInterval('PT0H5M0S')); $response->setExpires($exp); return $response; } catch (ItemNotFoundException $exception) { return new JsonResponse(['success' => 0]); } }
protected function respondWithArray($content = [], $status = 200, array $headers = []) { $response = new JsonResponse($content, $status, $headers); $response->setExpires(new \DateTime('1924-10-10 12:00:00 UTC')); $response->headers->addCacheControlDirective('no-cache', true); $response->headers->addCacheControlDirective('private', true); return $response; }