I'm creating a simple CMS with Symfony 4. Where it it's possible to create pages. When creating a page a Doctrine EvenSubscriber is being called. This subscriber creates an PageRoute.
<?php
#src/Doctrine/EvenListener
/**
* PageRouteSubscriber
*/
namespace App\Doctrine\EventListener;
use App\Controller\PageController;
use App\Entity\Page;
use App\Entity\Route;
use Doctrine\Common\EventSubscriber;
use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
/**
* Class PageRouteSubscriber
* @package App\Doctrine\EventListener
*/
class PageRouteSubscriber implements EventSubscriber {
/**
* Returns an array of events this subscriber wants to listen to.
* @return array
*/
public function getSubscribedEvents() {
return [
'postPersist',
'postUpdate',
];
}
public function postPersist(LifecycleEventArgs $args){
$this->index($args);
}
public function postUpdate(LifecycleEventArgs $args){
$this->index($args);
}
public function index(LifecycleEventArgs $args)
{
$entity = $args->getObject();
if($entity instanceof Page){
$route = new Route();
$route->setController(PageController::class);
$route->setRouteContentId(get_class($entity) . '#' . $entity->getId());
$route->setLocale($entity->getLocale());
$route->setSlug($entity->getSlug());
$entityManager = $args->getObjectManager();
$entityManager->persist($route);
$entityManager->flush();
}
}
}
So when the page is created there also is an route created. See images for database page and route examples.
Page
Route
I don't know this is the best way to store routes and pages in DB. Because they have both the same slug. I was thinking, only set slugs in route table. And per entity checking the unique slug based on all slugs in the route table (if this is possible?).
For routing: I don't now how to grep the routes and use them with Symfony Route Collection. Also is it possible to cache te routes just like Symfony does and created on big file called: srcDevDebugProjectContainerUrlGenerator
When i've got routes working i could create the frontend menu. Where a page menu item is coupled to an page. That page has an route. With this route the menu url could be created (is the the right way of thinking?).