/** * * @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()]); } }
static function defaultAction(Request $request, Service $service, $repositoryName) { $repository = $service->getRepository($repositoryName); if ($repository) { $response = new JsonResponse(); $etag = md5($repository->getLastModifiedDate()); $response->setEtag($etag); if ($response->isNotModified($request)) { $response->send(); return $response; } $result = []; $result['content'] = []; foreach ($repository->getContentTypeDefinitions() as $definition) { $result['content'][$definition->getName()]['title'] = $definition->getTitle() ? $definition->getTitle() : $definition->getName(); $result['content'][$definition->getName()]['lastchange_content'] = $repository->getLastModifiedDate($definition->getName()); $result['content'][$definition->getName()]['lastchange_cmdl'] = 0; // TODO } $response->setContent(json_encode($result, JSON_PRETTY_PRINT)); $response->setEtag($etag); $response->setPublic(); return $response; } }
/** * @Route("/{_locale}/get/seed/{seed}", name="get_by_seed_locale") * @Route("/get/seed/{seed}", name="get_by_seed") */ public function getBySeedAction(Request $request, $seed) { $return = array('status' => 1, 'message' => '', 'data' => null); // retorno padrão $router = $this->container->get('router'); $translator = $this->get('translator'); // da uma limpada $seed = md5($seed . $this->container->getParameter('secret')); // extrai um numero de 0~9 $seed = substr(base_convert(md5($seed), 16, 10), -1); srand($seed); // usa pro rand // image path $image_path = $request->getUriForPath('/image/cenouro.png'); $message = ''; // são tão poucas opções que nem vale a pena criar DB // balanceia as chances um pouco switch (rand(1, 4)) { case 1: $message = 'Yes!'; break; case 2: $message = 'No!'; break; default: switch (rand(1, 6)) { case 1: $message = 'Maybe.'; break; case 2: $message = 'Sure!'; break; case 3: $message = "I really don't care."; break; case 4: $message = "Of course not."; break; case 5: $message = "Ask again."; break; case 6: $message = "Screw you!"; break; } break; } // traduz, se precisar $message = $translator->trans($message); // responde $data = array('message' => $message, 'image' => $image_path); $return['data'] = $data; $response = new JsonResponse($return); // cache $response->setPublic(); $response->setSharedMaxAge(600000); $response->headers->addCacheControlDirective('must-revalidate', true); return $response; }
/** * @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 fetchAction(Request $request) { $id = $request->query->get('comment_id'); $feedName = 'comment_rating'; $params = array('comment_id' => $id); $comment = $this->getCommentById($id); if (!$comment) { $output = $this->presentResponse('Comment not found for id ' . $id, $feedName, $params); return new JsonResponse($output); } $ratingRepo = $this->getDoctrine()->getRepository('AppBundle:CommentRating'); $rating = $ratingRepo->getRatingForComment($id); $presenter = new Presenters\CommentRatingPresenter(); $data = $presenter->present(array('rating' => $rating[0])); $output = $this->presentResponse($data, 'comment_rating', $params); $resp = new JsonResponse($output); $resp->setPublic(); $resp->setMaxAge(5); return $resp; }
<?php require_once __DIR__ . '/../vendor/autoload.php'; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\JsonResponse; $app = new Silex\Application(); //$app['debug'] = true; // Twig $app->register(new Silex\Provider\TwigServiceProvider(), array('twig.path' => __DIR__ . '/../views')); // Cache $app->register(new Silex\Provider\HttpCacheServiceProvider(), array('http_cache.cache_dir' => __DIR__ . '/../cache/', 'http_cache.esi' => null)); // Weekend, where the magic happens. $app['weekend'] = function () { return new \Ternel\Weekend(); }; // Website $app->get('/', function () use($app) { $response = new Response($app['twig']->render('index.html.twig', ['text' => $app['weekend']->getText(), 'subtext' => $app['weekend']->getSubText()]), 200); $response->setPublic(); $response->setSharedMaxAge(60); return $response; }); // Api $app->get('/api', function () use($app) { $response = new JsonResponse(['text' => $app['weekend']->getText(), 'subtext' => $app['weekend']->getSubText()], 200); $response->setPublic(); $response->setSharedMaxAge(60); return $response; }); $app['http_cache']->run();