Skip to content

Instantly share code, notes, and snippets.

@cedricziel
Created November 30, 2016 15:10
Show Gist options
  • Save cedricziel/271d071b62125de39240883b937cf40a to your computer and use it in GitHub Desktop.
Save cedricziel/271d071b62125de39240883b937cf40a to your computer and use it in GitHub Desktop.
<?php
namespace AppBundle\Routing;
use AppBundle\Entity\RoutableEntityInterface;
use JMS\I18nRoutingBundle\Router\I18nRouter;
/**
* Class Router
* Overrides the I18n router to be able to resolve objects by route
* component.
*/
class Router extends I18nRouter
{
/**
* Converts doctrine entities so they can be used as arguments to the
* route generator.
*
* @param string $name
* @param array $parameters
* @param int $referenceType
*
* @return string
*/
public function generate($name, $parameters = [], $referenceType = self::ABSOLUTE_PATH)
{
foreach ($parameters as $paramName => $value) {
if ($value instanceof RoutableEntityInterface) {
if (method_exists($value, 'getSlug')) {
$parameters[$paramName] = $value->getSlug();
} elseif (method_exists($value, 'getId')) {
$parameters[$paramName] = $value->getId();
}
}
}
return parent::generate($name, $parameters, $referenceType);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment