RouteBundle
===========
The RouteBundle provides the feature of managing website-routes for
custom-entities. These routes will be generated by calling the
`RouteManager` which uses a configurable schema for creating the
route-path.
Every Entity can be combined with a route. You only have to call the
`create` method of the service `sulu_route.manager.route_manager`
with an entity implementing the `RoutableInterface`.
The `update` method will update the route-path with the configured
schema and hold "history" (301 redirects) routes for the old routes.
To resolve the route on request you have to provide a
`RouteDefaultsProvider` which defines the controller and attributes
of the route.
Further Topics
--------------
.. toctree::
:maxdepth: 1
custom_route_generator
Example with RoutableEntity
---------------------------
This example will create a simple example-entity which will be
available on the website with an own route.
Entity
******
First extend your entity with the properties `id`, `locale` and a new many-to-one relation `route`:
event.orm.xml:
.. code-block:: xml
...
Event.php:
.. code-block:: php
namespace App\Entity;
use Sulu\Bundle\RouteBundle\Model\RoutableInterface;
use Sulu\Bundle\RouteBundle\Model\RouteInterface;
class Event implements RoutableInterface
{
/**
* @var string
*/
private $title;
/**
* @var RouteInterface
*/
private $route;
public function getTitle() { ... }
public function setTitle() { ... }
/**
* {@inheritdoc}
*/
public function getId() { ... }
/**
* {@inheritdoc}
*/
public function getLocale() { ... }
/**
* {@inheritdoc}
*/
public function setRoute(RouteInterface $route) { ... }
/**
* {@inheritdoc}
*/
public function getRoute() { ... }
}
Route-Schema
************
Configure the route-schema in the file `config/packages/sulu_route.yml`:
.. code-block:: yaml
sulu_route:
mappings:
App\Entity\Event:
generator: schema
options:
route_schema: /{translator.trans('event')}/{object.getTitle()}
resource_key: events
.. note::
You can use the `translator` in the schema to create translated routes.
EventController
****************
.. code-block:: php
namespace App\Controller;
use App\Entity\Event;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class EventController extends AbstractController
{
public function indexAction(Event $event)
{
return $this->render('events/event.html.twig', ['event' => $event]);
}
}
RouteDefaultsProvider
*********************
.. code-block:: php
namespace App\Routing;
use App\Entity\Event;
use App\Entity\EventRepository;
use Sulu\Bundle\RouteBundle\Routing\Defaults\RouteDefaultsProviderInterface;
class EventRouteDefaultsProvider implements RouteDefaultsProviderInterface
{
protected $eventRepository;
public function __construct(EventRepository $eventRepository)
{
$this->eventRepository = $eventRepository;
}
public function getByEntity($entityClass, $id, $locale, $object = null)
{
return [
'_controller' => 'App\Controller\EventController::indexAction',
'event' => $object ?: $this->eventRepository->find($id, $locale),
];
}
public function isPublished($entityClass, $id, $locale)
{
return true;
}
public function supports($entityClass)
{
return $entityClass === Event::class;
}
}
Register this class as a service with the tag ``.
After that the entity is ready to get a route. To create a route for the new entities
simple call the `save` method of the service `sulu_route.manager.route_manager`.
.. note::
To update already existing entities you can run the command
`php bin/console sulu:route:update App\\Entity\\Event` which updates or creates
the route for all the entities of this type.
Example without RoutableEntity
------------------------------
If it is not possible to implement the `RoutableInterface` you can call the method
`createOrUpdateByAttributes($entityClass, $id, $locale, $path)` of the service
`sulu_route.manager.route_manager`.
This method does not handle route generation - for this reason you can also omit the
generator and the `options` in the mapping configuration.
.. code-block:: yaml
sulu_route:
mappings:
App\Entity\Event:
resource_key: events
As there is no cascading on database layer you have to call the `remove` method on the
service `sulu.repository.route` when you remove your entity.