Implemented nice urls

This commit is contained in:
Tim Schumacher 2014-07-10 12:43:09 +02:00
parent 4d3ad6ea2e
commit e17c6529db
7 changed files with 125 additions and 25 deletions

View file

@ -3,6 +3,7 @@
namespace Hackspace\Bundle\CalciferBundle\Controller; namespace Hackspace\Bundle\CalciferBundle\Controller;
use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder; use Doctrine\ORM\QueryBuilder;
use Hackspace\Bundle\CalciferBundle\Entity\Location; use Hackspace\Bundle\CalciferBundle\Entity\Location;
use Hackspace\Bundle\CalciferBundle\Entity\Tag; use Hackspace\Bundle\CalciferBundle\Entity\Tag;
@ -49,7 +50,7 @@ class EventController extends Controller
/** /**
* Creates a new Event entity. * Creates a new Event entity.
* *
* @Route("/", name="_create") * @Route("/termine/", name="_create")
* @Method("POST") * @Method("POST")
* @Template("CalciferBundle:Event:new.html.twig") * @Template("CalciferBundle:Event:new.html.twig")
*/ */
@ -62,6 +63,7 @@ class EventController extends Controller
$startdate = $request->get('startdate'); $startdate = $request->get('startdate');
$startdate = new \DateTime($startdate); $startdate = new \DateTime($startdate);
$entity->setStartdate($startdate); $entity->setStartdate($startdate);
$entity->setSlug(\URLify::filter($entity->getSummary(),255,'de'));
$enddate = $request->get('enddate'); $enddate = $request->get('enddate');
if (strlen($enddate) > 0) { if (strlen($enddate) > 0) {
@ -90,6 +92,7 @@ class EventController extends Controller
$location_obj->setName($location); $location_obj->setName($location);
$location_obj->setLat($location_lat); $location_obj->setLat($location_lat);
$location_obj->setLon($location_lon); $location_obj->setLon($location_lon);
$location_obj->setSlug(\URLify::filter($location_obj->getName(),255,'de'));
$em->persist($location_obj); $em->persist($location_obj);
$em->flush(); $em->flush();
$entity->setLocation($location_obj); $entity->setLocation($location_obj);
@ -109,6 +112,7 @@ class EventController extends Controller
} else { } else {
$tag_obj = new Tag(); $tag_obj = new Tag();
$tag_obj->setName($tag); $tag_obj->setName($tag);
$tag_obj->setSlug(\URLify::filter($tag_obj->getName(),255,'de'));
$em->persist($tag_obj); $em->persist($tag_obj);
$em->flush(); $em->flush();
$entity->addTag($tag_obj); $entity->addTag($tag_obj);
@ -122,7 +126,7 @@ class EventController extends Controller
$em->persist($entity); $em->persist($entity);
$em->flush(); $em->flush();
return $this->redirect($this->generateUrl('_show', array('id' => $entity->getId()))); return $this->redirect($this->generateUrl('_show', array('slug' => $entity->getSlug())));
} }
return array( return array(
@ -133,7 +137,7 @@ class EventController extends Controller
/** /**
* Displays a form to create a new Event entity. * Displays a form to create a new Event entity.
* *
* @Route("/new", name="_new") * @Route("/termine/neu", name="_new")
* @Method("GET") * @Method("GET")
* @Template() * @Template()
*/ */
@ -149,15 +153,20 @@ class EventController extends Controller
/** /**
* Finds and displays a Event entity. * Finds and displays a Event entity.
* *
* @Route("/{id}", name="_show") * @Route("/termine/{slug}", name="_show")
* @Method("GET") * @Method("GET")
* @Template() * @Template()
*/ */
public function showAction($id) public function showAction($slug)
{ {
/** @var EntityManager $em */
$em = $this->getDoctrine()->getManager(); $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) { if (!$entity) {
throw $this->createNotFoundException('Unable to find Event 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. * Displays a form to edit an existing Event entity.
* *
* @Route("/{id}/edit", name="_edit") * @Route("/termine/{slug}/edit", name="_edit")
* @Method("GET") * @Method("GET")
* @Template() * @Template()
*/ */
public function editAction($id) public function editAction($slug)
{ {
/** @var EntityManager $em */
$em = $this->getDoctrine()->getManager(); $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) { if (!$entity) {
throw $this->createNotFoundException('Unable to find Event entity.'); throw $this->createNotFoundException('Unable to find Event entity.');
@ -193,16 +207,20 @@ class EventController extends Controller
/** /**
* Edits an existing Event entity. * Edits an existing Event entity.
* *
* @Route("/{id}", name="_update") * @Route("/termine/{slug}", name="_update")
* @Method("POST") * @Method("POST")
* @Template("CalciferBundle:Event:edit.html.twig") * @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(); $em = $this->getDoctrine()->getManager();
/** @var EntityRepository $repo */
$repo = $em->getRepository('CalciferBundle:Event');
/** @var Event $entity */ /** @var Event $entity */
$entity = $em->getRepository('CalciferBundle:Event')->find($id); $entity = $repo->findOneBy(['slug' => $slug]);
if (!$entity) { if (!$entity) {
throw $this->createNotFoundException('Unable to find Event entity.'); throw $this->createNotFoundException('Unable to find Event entity.');
@ -214,6 +232,7 @@ class EventController extends Controller
$startdate = $request->get('startdate'); $startdate = $request->get('startdate');
$startdate = new \DateTime($startdate); $startdate = new \DateTime($startdate);
$entity->setStartdate($startdate); $entity->setStartdate($startdate);
$entity->setSlug(\URLify::filter($entity->getSummary(),255,'de'));
$enddate = $request->get('enddate'); $enddate = $request->get('enddate');
if (strlen($enddate) > 0) { if (strlen($enddate) > 0) {
@ -242,6 +261,7 @@ class EventController extends Controller
$location_obj->setName($location); $location_obj->setName($location);
$location_obj->setLat($location_lat); $location_obj->setLat($location_lat);
$location_obj->setLon($location_lon); $location_obj->setLon($location_lon);
$location_obj->setSlug(\URLify::filter($location_obj->getName(),255,'de'));
$em->persist($location_obj); $em->persist($location_obj);
$em->flush(); $em->flush();
$entity->setLocation($location_obj); $entity->setLocation($location_obj);
@ -261,6 +281,7 @@ class EventController extends Controller
} else { } else {
$tag_obj = new Tag(); $tag_obj = new Tag();
$tag_obj->setName($tag); $tag_obj->setName($tag);
$tag_obj->setSlug(\URLify::filter($tag_obj->getName(),255,'de'));
$em->persist($tag_obj); $em->persist($tag_obj);
$em->flush(); $em->flush();
$entity->addTag($tag_obj); $entity->addTag($tag_obj);
@ -274,7 +295,7 @@ class EventController extends Controller
$em->persist($entity); $em->persist($entity);
$em->flush(); $em->flush();
return $this->redirect($this->generateUrl('_show', array('id' => $entity->getId()))); return $this->redirect($this->generateUrl('_show', array('slug' => $entity->getSlug())));
} }
return array( return array(

View file

@ -2,6 +2,8 @@
namespace Hackspace\Bundle\CalciferBundle\Controller; namespace Hackspace\Bundle\CalciferBundle\Controller;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder; use Doctrine\ORM\QueryBuilder;
use Hackspace\Bundle\CalciferBundle\Entity\Location; use Hackspace\Bundle\CalciferBundle\Entity\Location;
use Hackspace\Bundle\CalciferBundle\Entity\Tag; use Hackspace\Bundle\CalciferBundle\Entity\Tag;
@ -26,23 +28,27 @@ use Symfony\Component\Validator\Constraints\DateTime;
/** /**
* Location controller. * Location controller.
* *
* @Route("/locations") * @Route("/orte")
*/ */
class LocationController extends Controller class LocationController extends Controller
{ {
/** /**
* Finds and displays a Event entity. * Finds and displays a Event entity.
* *
* @Route("/{id}", requirements={"id" = "\d+"}, name="location_show") * @Route("/{slug}", name="location_show")
* @Method("GET") * @Method("GET")
* @Template("CalciferBundle:Event:index.html.twig") * @Template("CalciferBundle:Event:index.html.twig")
*/ */
public function showAction($id) public function showAction($slug)
{ {
/** @var EntityManager $em */
$em = $this->getDoctrine()->getManager(); $em = $this->getDoctrine()->getManager();
/** @var EntityRepository $repo */
$repo = $em->getRepository('CalciferBundle:Location');
/** @var Location $location */ /** @var Location $location */
$location = $em->getRepository('CalciferBundle:Location')->find($id); $location = $repo->findOneBy(['slug' => $slug]);
if (!$location) { if (!$location) {
throw $this->createNotFoundException('Unable to find Location entity.'); throw $this->createNotFoundException('Unable to find Location entity.');

View file

@ -3,6 +3,7 @@
namespace Hackspace\Bundle\CalciferBundle\Controller; namespace Hackspace\Bundle\CalciferBundle\Controller;
use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder; use Doctrine\ORM\QueryBuilder;
use Hackspace\Bundle\CalciferBundle\Entity\Location; use Hackspace\Bundle\CalciferBundle\Entity\Location;
use Hackspace\Bundle\CalciferBundle\Entity\Tag; use Hackspace\Bundle\CalciferBundle\Entity\Tag;
@ -34,16 +35,20 @@ class TagController extends Controller
/** /**
* Finds and displays a Event entity. * Finds and displays a Event entity.
* *
* @Route("/{id}", requirements={"id" = "\d+"}, name="tag_show") * @Route("/{slug}", name="tag_show")
* @Method("GET") * @Method("GET")
* @Template("CalciferBundle:Event:index.html.twig") * @Template("CalciferBundle:Event:index.html.twig")
*/ */
public function showAction($id) public function showAction($slug)
{ {
/** @var EntityManager $em */
$em = $this->getDoctrine()->getManager(); $em = $this->getDoctrine()->getManager();
/** @var Tag $tag */ /** @var EntityRepository $repo */
$tag = $em->getRepository('CalciferBundle:Tag')->find($id); $repo = $em->getRepository('CalciferBundle:Tag');
/** @var Tag $location */
$tag = $repo->findOneBy(['slug' => $slug]);
if (!$tag) { if (!$tag) {
throw $this->createNotFoundException('Unable to find tag entity.'); throw $this->createNotFoundException('Unable to find tag entity.');

View file

@ -83,6 +83,29 @@ class Event
*/ */
private $tags = []; 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 * Get id

View file

@ -42,6 +42,29 @@ class Location
*/ */
private $lat; 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 * Get id

View file

@ -28,6 +28,28 @@ class Tag
*/ */
private $name; 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 * Get id

View file

@ -1,10 +1,10 @@
<div class="ui column"> <div class="ui column">
<div class="ui segment event box"> <div class="ui segment event box">
<h2 class="ui dividing header segment green inverted"><a href="{{ path('_show', { 'id': entity.id }) }}">{{ entity.summary }}</a> <h2 class="ui dividing header segment green inverted"><a href="{{ path('_show', { 'slug': entity.slug }) }}">{{ entity.summary }}</a>
</h2> </h2>
<p class="edit"> <p class="edit">
<a href="{{ path('_edit', {'id' : entity.id}) }}"><i class="ui icon edit circular green inverted"></i>Bearbeiten</a> <a href="{{ path('_edit', {'slug' : entity.slug }) }}"><i class="ui icon edit circular green inverted"></i>Bearbeiten</a>
</p> </p>
<p class="startdate "> <p class="startdate ">
@ -13,13 +13,13 @@
{% if entity.location is not null %} {% if entity.location is not null %}
<p class="location"> <p class="location">
<a href="{{ path('location_show', {'id' : entity.location.id}) }}"><i class="ui icon map marker circular green inverted link" title="Wo?" data-content="Wo?"></i>{{ entity.location.name }}</a> <a href="{{ path('location_show', {'slug' : entity.location.slug }) }}"><i class="ui icon map marker circular green inverted link" title="Wo?" data-content="Wo?"></i>{{ entity.location.name }}</a>
</p> </p>
{% endif %} {% endif %}
{% if entity.tags|length > 0 %} {% if entity.tags|length > 0 %}
<ul class="tags"> <ul class="tags">
{% for tag in entity.tags %} {% for tag in entity.tags %}
<li class="tag"><a href="{{ path('tag_show',{'id' : tag.id}) }}"><i class="circular icon tag green inverted"></i>{{ tag.name }}</a></li> <li class="tag"><a href="{{ path('tag_show',{'slug' : tag.slug }) }}"><i class="circular icon tag green inverted"></i>{{ tag.name }}</a></li>
{% endfor %} {% endfor %}
</ul> </ul>
{% endif %} {% endif %}