ApiController.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435
  1. <?php
  2. namespace JeroenED\PortfolioBundle\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  4. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  5. use Symfony\Component\Security\Core\User\UserInterface;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  8. use JeroenED\PortfolioBundle\Entity\Page;
  9. /**
  10. * Description of ApiController
  11. *
  12. * @author Jeroen De Meerleer <me@jeroened.be>
  13. */
  14. class ApiController extends Controller {
  15. /**
  16. * @Route("/api/getPage/{slug}", defaults={ "slug" = "none"})
  17. */
  18. public function getPageAction($slug, UserInterface $user = null)
  19. {
  20. if($slug == 'none') throw new NotFoundHttpException('Page not found');
  21. $loggedin = null !== $user ? true : false;
  22. $whereclause = "p.slug LIKE :slug";
  23. $whereclause .= (!$loggedin) ? " AND online = true": "";
  24. $page = $this->getDoctrine()->getRepository(Page::class)
  25. ->createQueryBuilder('p')
  26. ->where($whereclause)
  27. ->setParameter('slug', '%;'.$slug.';%')
  28. ->getQuery()->getResult();
  29. if ($page == null ) throw new NotFoundHttpException('Page not found');
  30. return new Response("<h1>" . $page[0]->getTitle() . "</h1>" . $page[0]->getHtml());
  31. }
  32. }