diff --git a/Resources/config/services.xml b/Resources/config/services.xml index a5c3f66..cf8c4d1 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -16,6 +16,7 @@ JMS\I18nRoutingBundle\EventListener\CookieSettingListener JMS\I18nRoutingBundle\Translation\RouteTranslationExtractor + JMS\I18nRoutingBundle\Twig\I18nRoutingExtension @@ -37,6 +38,9 @@ %jms_i18n_routing.redirect_to_host% + + + @@ -69,5 +73,11 @@ + + + + %jms_i18n_routing.locales% + + diff --git a/Resources/views/hreflang.html.twig b/Resources/views/hreflang.html.twig new file mode 100644 index 0000000..cbabc37 --- /dev/null +++ b/Resources/views/hreflang.html.twig @@ -0,0 +1,5 @@ + +{% for locale in locales %} + {% set arr = routeParams|merge({'_locale':locale}) %} + +{% endfor %} diff --git a/Router/I18nLoader.php b/Router/I18nLoader.php index c6148dd..0e67e84 100644 --- a/Router/I18nLoader.php +++ b/Router/I18nLoader.php @@ -63,6 +63,7 @@ public function load(RouteCollection $collection) $catchMultipleRoute = clone $route; $catchMultipleRoute->setPath($pattern); $catchMultipleRoute->setDefault('_locales', $locales); + $catchMultipleRoute->setDefault('localized', true); $i18nCollection->add(implode('_', $locales).I18nLoader::ROUTING_PREFIX.$name, $catchMultipleRoute); } @@ -70,6 +71,7 @@ public function load(RouteCollection $collection) $localeRoute = clone $route; $localeRoute->setPath($pattern); $localeRoute->setDefault('_locale', $locale); + $localeRoute->setDefault('localized', true); $i18nCollection->add($locale.I18nLoader::ROUTING_PREFIX.$name, $localeRoute); } } diff --git a/Router/I18nRouter.php b/Router/I18nRouter.php index 42dd248..e61181d 100644 --- a/Router/I18nRouter.php +++ b/Router/I18nRouter.php @@ -20,6 +20,9 @@ use JMS\I18nRoutingBundle\Exception\NotAcceptableLanguageException; +use Psr\Log\LoggerAwareInterface; +use Psr\Log\LoggerAwareTrait; +use Psr\Log\LoggerInterface; use Symfony\Component\Routing\Matcher\RequestMatcherInterface; use Symfony\Component\Routing\RequestContext; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -33,7 +36,7 @@ * * @author Johannes M. Schmitt */ -class I18nRouter extends Router +class I18nRouter extends Router implements LoggerAwareInterface { private $hostMap = array(); private $i18nLoaderId; @@ -140,8 +143,21 @@ public function generate($name, $parameters = array(), $referenceType = self::AB // fallback to default behavior } - // use the default behavior if no localized route exists - return $generator->generate($name, $parameters, $referenceType); + try { + // use the default behavior if no localized route exists + return $generator->generate($name, $parameters, $referenceType); + } catch (\Exception $e) { + if ($this->logger) { + $this->logger->critical('JMSRouter tried to generate a route', [ + 'category'=>'routing', + 'name'=>$name, + 'parameters'=>json_encode($parameters), + 'absolute'=>$absolute ? 'true' : 'false', + ]); + } + + throw $e; + } } /** @@ -290,4 +306,15 @@ private function getRequest() return $request; } + + + /** + * Sets a logger. + * + * @param LoggerInterface $logger + */ + public function setLogger(LoggerInterface $logger) + { + $this->logger = $logger; + } } diff --git a/Twig/I18nRoutingExtension.php b/Twig/I18nRoutingExtension.php new file mode 100644 index 0000000..fdde736 --- /dev/null +++ b/Twig/I18nRoutingExtension.php @@ -0,0 +1,77 @@ +requestStack = $requestStack; + $this->locales = $locales; + } + + public function getFunctions() + { + return array( + new \Twig_SimpleFunction('hreflang', array($this, 'getHreflang'), array( + 'needs_environment'=>true, + 'is_safe'=>array('html'), + )), + ); + } + + /** + * Return HTML with hreflang attributes + * + * @param \Twig_Environment $env + */ + public function getHreflang(\Twig_Environment $env) + { + if (null === $request = $this->requestStack->getMasterRequest()) { + return; + } + + if (null === $routeParams = $request->attributes->get('_route_params')) { + return; + } + + if (!isset($routeParams['localized']) || !$routeParams['localized']) { + return; + } + + return $env->render('JMSI18nRoutingBundle::hreflang.html.twig', array( + 'locales'=>$this->locales, + 'route'=>$request->attributes->get('_route'), + 'routeParams'=>$routeParams, + )); + } + + /** + * {@inheritdoc} + * + * @return string + */ + public function getName() + { + return 'i18n_routing_extension'; + } +} \ No newline at end of file diff --git a/composer.json b/composer.json index be9a7aa..855e0ba 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ } ], "require": { - "php": ">=5.3.9", + "php": ">=5.3.9", "symfony/framework-bundle": "~2.3|~3.0" }, "require-dev": {