Created
November 30, 2016 15:10
-
-
Save cedricziel/271d071b62125de39240883b937cf40a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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