From e17c6529db5e0e3cc60fa78300e8e61d11860a63 Mon Sep 17 00:00:00 2001 From: Tim Schumacher Date: Thu, 10 Jul 2014 12:43:09 +0200 Subject: [PATCH] Implemented nice urls --- .../Controller/EventController.php | 47 ++++++++++++++----- .../Controller/LocationController.php | 14 ++++-- .../Controller/TagController.php | 13 +++-- .../Bundle/CalciferBundle/Entity/Event.php | 23 +++++++++ .../Bundle/CalciferBundle/Entity/Location.php | 23 +++++++++ .../Bundle/CalciferBundle/Entity/Tag.php | 22 +++++++++ .../Resources/views/Event/event_box.html.twig | 8 ++-- 7 files changed, 125 insertions(+), 25 deletions(-) diff --git a/src/Hackspace/Bundle/CalciferBundle/Controller/EventController.php b/src/Hackspace/Bundle/CalciferBundle/Controller/EventController.php index 24baffa..20f3c6d 100755 --- a/src/Hackspace/Bundle/CalciferBundle/Controller/EventController.php +++ b/src/Hackspace/Bundle/CalciferBundle/Controller/EventController.php @@ -3,6 +3,7 @@ namespace Hackspace\Bundle\CalciferBundle\Controller; use Doctrine\ORM\EntityManager; +use Doctrine\ORM\EntityRepository; use Doctrine\ORM\QueryBuilder; use Hackspace\Bundle\CalciferBundle\Entity\Location; use Hackspace\Bundle\CalciferBundle\Entity\Tag; @@ -49,7 +50,7 @@ class EventController extends Controller /** * Creates a new Event entity. * - * @Route("/", name="_create") + * @Route("/termine/", name="_create") * @Method("POST") * @Template("CalciferBundle:Event:new.html.twig") */ @@ -62,6 +63,7 @@ class EventController extends Controller $startdate = $request->get('startdate'); $startdate = new \DateTime($startdate); $entity->setStartdate($startdate); + $entity->setSlug(\URLify::filter($entity->getSummary(),255,'de')); $enddate = $request->get('enddate'); if (strlen($enddate) > 0) { @@ -90,6 +92,7 @@ class EventController extends Controller $location_obj->setName($location); $location_obj->setLat($location_lat); $location_obj->setLon($location_lon); + $location_obj->setSlug(\URLify::filter($location_obj->getName(),255,'de')); $em->persist($location_obj); $em->flush(); $entity->setLocation($location_obj); @@ -109,6 +112,7 @@ class EventController extends Controller } else { $tag_obj = new Tag(); $tag_obj->setName($tag); + $tag_obj->setSlug(\URLify::filter($tag_obj->getName(),255,'de')); $em->persist($tag_obj); $em->flush(); $entity->addTag($tag_obj); @@ -122,7 +126,7 @@ class EventController extends Controller $em->persist($entity); $em->flush(); - return $this->redirect($this->generateUrl('_show', array('id' => $entity->getId()))); + return $this->redirect($this->generateUrl('_show', array('slug' => $entity->getSlug()))); } return array( @@ -133,7 +137,7 @@ class EventController extends Controller /** * Displays a form to create a new Event entity. * - * @Route("/new", name="_new") + * @Route("/termine/neu", name="_new") * @Method("GET") * @Template() */ @@ -149,15 +153,20 @@ class EventController extends Controller /** * Finds and displays a Event entity. * - * @Route("/{id}", name="_show") + * @Route("/termine/{slug}", name="_show") * @Method("GET") * @Template() */ - public function showAction($id) + public function showAction($slug) { + /** @var EntityManager $em */ $em = $this->getDoctrine()->getManager(); - $entity = $em->getRepository('CalciferBundle:Event')->find($id); + /** @var EntityRepository $repo */ + $repo = $em->getRepository('CalciferBundle:Event'); + + /** @var Event $entity */ + $entity = $repo->findOneBy(['slug' => $slug]); if (!$entity) { throw $this->createNotFoundException('Unable to find Event entity.'); @@ -171,15 +180,20 @@ class EventController extends Controller /** * Displays a form to edit an existing Event entity. * - * @Route("/{id}/edit", name="_edit") + * @Route("/termine/{slug}/edit", name="_edit") * @Method("GET") * @Template() */ - public function editAction($id) + public function editAction($slug) { + /** @var EntityManager $em */ $em = $this->getDoctrine()->getManager(); - $entity = $em->getRepository('CalciferBundle:Event')->find($id); + /** @var EntityRepository $repo */ + $repo = $em->getRepository('CalciferBundle:Event'); + + /** @var Event $entity */ + $entity = $repo->findOneBy(['slug' => $slug]); if (!$entity) { throw $this->createNotFoundException('Unable to find Event entity.'); @@ -193,16 +207,20 @@ class EventController extends Controller /** * Edits an existing Event entity. * - * @Route("/{id}", name="_update") + * @Route("/termine/{slug}", name="_update") * @Method("POST") * @Template("CalciferBundle:Event:edit.html.twig") */ - public function updateAction(Request $request, $id) + public function updateAction(Request $request, $slug) { + /** @var EntityManager $em */ $em = $this->getDoctrine()->getManager(); + /** @var EntityRepository $repo */ + $repo = $em->getRepository('CalciferBundle:Event'); + /** @var Event $entity */ - $entity = $em->getRepository('CalciferBundle:Event')->find($id); + $entity = $repo->findOneBy(['slug' => $slug]); if (!$entity) { throw $this->createNotFoundException('Unable to find Event entity.'); @@ -214,6 +232,7 @@ class EventController extends Controller $startdate = $request->get('startdate'); $startdate = new \DateTime($startdate); $entity->setStartdate($startdate); + $entity->setSlug(\URLify::filter($entity->getSummary(),255,'de')); $enddate = $request->get('enddate'); if (strlen($enddate) > 0) { @@ -242,6 +261,7 @@ class EventController extends Controller $location_obj->setName($location); $location_obj->setLat($location_lat); $location_obj->setLon($location_lon); + $location_obj->setSlug(\URLify::filter($location_obj->getName(),255,'de')); $em->persist($location_obj); $em->flush(); $entity->setLocation($location_obj); @@ -261,6 +281,7 @@ class EventController extends Controller } else { $tag_obj = new Tag(); $tag_obj->setName($tag); + $tag_obj->setSlug(\URLify::filter($tag_obj->getName(),255,'de')); $em->persist($tag_obj); $em->flush(); $entity->addTag($tag_obj); @@ -274,7 +295,7 @@ class EventController extends Controller $em->persist($entity); $em->flush(); - return $this->redirect($this->generateUrl('_show', array('id' => $entity->getId()))); + return $this->redirect($this->generateUrl('_show', array('slug' => $entity->getSlug()))); } return array( diff --git a/src/Hackspace/Bundle/CalciferBundle/Controller/LocationController.php b/src/Hackspace/Bundle/CalciferBundle/Controller/LocationController.php index 0b13bbb..7924376 100755 --- a/src/Hackspace/Bundle/CalciferBundle/Controller/LocationController.php +++ b/src/Hackspace/Bundle/CalciferBundle/Controller/LocationController.php @@ -2,6 +2,8 @@ namespace Hackspace\Bundle\CalciferBundle\Controller; +use Doctrine\ORM\EntityManager; +use Doctrine\ORM\EntityRepository; use Doctrine\ORM\QueryBuilder; use Hackspace\Bundle\CalciferBundle\Entity\Location; use Hackspace\Bundle\CalciferBundle\Entity\Tag; @@ -26,23 +28,27 @@ use Symfony\Component\Validator\Constraints\DateTime; /** * Location controller. * - * @Route("/locations") + * @Route("/orte") */ class LocationController extends Controller { /** * Finds and displays a Event entity. * - * @Route("/{id}", requirements={"id" = "\d+"}, name="location_show") + * @Route("/{slug}", name="location_show") * @Method("GET") * @Template("CalciferBundle:Event:index.html.twig") */ - public function showAction($id) + public function showAction($slug) { + /** @var EntityManager $em */ $em = $this->getDoctrine()->getManager(); + /** @var EntityRepository $repo */ + $repo = $em->getRepository('CalciferBundle:Location'); + /** @var Location $location */ - $location = $em->getRepository('CalciferBundle:Location')->find($id); + $location = $repo->findOneBy(['slug' => $slug]); if (!$location) { throw $this->createNotFoundException('Unable to find Location entity.'); diff --git a/src/Hackspace/Bundle/CalciferBundle/Controller/TagController.php b/src/Hackspace/Bundle/CalciferBundle/Controller/TagController.php index c0013d7..6d0a55e 100755 --- a/src/Hackspace/Bundle/CalciferBundle/Controller/TagController.php +++ b/src/Hackspace/Bundle/CalciferBundle/Controller/TagController.php @@ -3,6 +3,7 @@ namespace Hackspace\Bundle\CalciferBundle\Controller; use Doctrine\ORM\EntityManager; +use Doctrine\ORM\EntityRepository; use Doctrine\ORM\QueryBuilder; use Hackspace\Bundle\CalciferBundle\Entity\Location; use Hackspace\Bundle\CalciferBundle\Entity\Tag; @@ -34,16 +35,20 @@ class TagController extends Controller /** * Finds and displays a Event entity. * - * @Route("/{id}", requirements={"id" = "\d+"}, name="tag_show") + * @Route("/{slug}", name="tag_show") * @Method("GET") * @Template("CalciferBundle:Event:index.html.twig") */ - public function showAction($id) + public function showAction($slug) { + /** @var EntityManager $em */ $em = $this->getDoctrine()->getManager(); - /** @var Tag $tag */ - $tag = $em->getRepository('CalciferBundle:Tag')->find($id); + /** @var EntityRepository $repo */ + $repo = $em->getRepository('CalciferBundle:Tag'); + + /** @var Tag $location */ + $tag = $repo->findOneBy(['slug' => $slug]); if (!$tag) { throw $this->createNotFoundException('Unable to find tag entity.'); diff --git a/src/Hackspace/Bundle/CalciferBundle/Entity/Event.php b/src/Hackspace/Bundle/CalciferBundle/Entity/Event.php index 6f340cc..82dfc4e 100755 --- a/src/Hackspace/Bundle/CalciferBundle/Entity/Event.php +++ b/src/Hackspace/Bundle/CalciferBundle/Entity/Event.php @@ -83,6 +83,29 @@ class Event */ private $tags = []; + /** + * @var string + * + * @ORM\Column(name="slug", type="string", length=255,options={"default" = ""}) + */ + private $slug = ''; + + /** + * @param string $slug + */ + public function setSlug($slug) + { + $this->slug = $slug; + } + + /** + * @return string + */ + public function getSlug() + { + return $this->slug; + } + /** * Get id diff --git a/src/Hackspace/Bundle/CalciferBundle/Entity/Location.php b/src/Hackspace/Bundle/CalciferBundle/Entity/Location.php index 5720ef5..8d3b6a2 100755 --- a/src/Hackspace/Bundle/CalciferBundle/Entity/Location.php +++ b/src/Hackspace/Bundle/CalciferBundle/Entity/Location.php @@ -42,6 +42,29 @@ class Location */ private $lat; + /** + * @var string + * + * @ORM\Column(name="slug", type="string", length=255,options={"default" = ""}) + */ + private $slug = ''; + + /** + * @param string $slug + */ + public function setSlug($slug) + { + $this->slug = $slug; + } + + /** + * @return string + */ + public function getSlug() + { + return $this->slug; + } + /** * Get id diff --git a/src/Hackspace/Bundle/CalciferBundle/Entity/Tag.php b/src/Hackspace/Bundle/CalciferBundle/Entity/Tag.php index 0223d3f..ae4ad82 100755 --- a/src/Hackspace/Bundle/CalciferBundle/Entity/Tag.php +++ b/src/Hackspace/Bundle/CalciferBundle/Entity/Tag.php @@ -28,6 +28,28 @@ class Tag */ private $name; + /** + * @var string + * + * @ORM\Column(name="slug", type="string", length=255,options={"default" = ""}) + */ + private $slug = ''; + + /** + * @param string $slug + */ + public function setSlug($slug) + { + $this->slug = $slug; + } + + /** + * @return string + */ + public function getSlug() + { + return $this->slug; + } /** * Get id diff --git a/src/Hackspace/Bundle/CalciferBundle/Resources/views/Event/event_box.html.twig b/src/Hackspace/Bundle/CalciferBundle/Resources/views/Event/event_box.html.twig index 1ce6bad..5b67deb 100755 --- a/src/Hackspace/Bundle/CalciferBundle/Resources/views/Event/event_box.html.twig +++ b/src/Hackspace/Bundle/CalciferBundle/Resources/views/Event/event_box.html.twig @@ -1,10 +1,10 @@
-

{{ entity.summary }} +

{{ entity.summary }}

- Bearbeiten + Bearbeiten

@@ -13,13 +13,13 @@ {% if entity.location is not null %}

- {{ entity.location.name }} + {{ entity.location.name }}

{% endif %} {% if entity.tags|length > 0 %} {% endif %}