Last active
December 23, 2022 03:59
-
-
Save kunicmarko20/02a42c76f638322d58b1def7d2e770d7 to your computer and use it in GitHub Desktop.
Revisions
-
kunicmarko20 revised this gist
Apr 29, 2018 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -34,7 +34,7 @@ public function __construct(RouterInterface $router, $languages, $defaultLanguag $this->languages = $languages; $this->defaultLanguage = $defaultLanguage; $context = new RequestContext("/"); } public function onKernelRequest(GetResponseEvent $event) { -
kunicmarko20 revised this gist
Apr 21, 2017 . 3 changed files with 16 additions and 20 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,23 +1,3 @@ <?php namespace YourBundle\EventListener; 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,6 @@ language_routing: resource: "@YourBundle/Resources/config/multilanguage-routes.yml" prefix: /{_locale} requirements: _locale: de|en defaults: { _locale: de} 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,10 @@ parameters: locale: de languages: [de, en] services: app.locale.listener: class: YourBundle\EventListener\LocaleListener arguments: ["@router","%languages%","%locale%"] tags: - { name: kernel.event_subscriber } -
kunicmarko20 revised this gist
Feb 7, 2017 . 1 changed file with 25 additions and 19 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,9 +1,14 @@ ////////////////////////////////////////////// //parameters: // locale: de // languages: [de, en] // //services: // app.locale.listener: // class: YourBundle\EventListener\LocaleListener // arguments: ["@router","%languages%","%locale%"] // tags: // - { name: kernel.event_subscriber } // in default routing import your routing file with routes that have multilanguage //language_routing: @@ -37,16 +42,19 @@ class LocaleListener implements EventSubscriberInterface */ private $urlMatcher; private $oldUrl; private $newUrl; private $languages; private $defaultLanguage; public function __construct(RouterInterface $router, $languages, $defaultLanguage = 'de') { $this->routeCollection = $router->getRouteCollection(); $this->languages = $languages; $this->defaultLanguage = $defaultLanguage; $context = new RequestContext("/"); public function onKernelRequest(GetResponseEvent $event) { @@ -76,17 +84,15 @@ public function onKernelRequest(GetResponseEvent $event) } } private function checkLanguage(){ foreach($this->languages as $language){ if(preg_match_all("/\/$language\//", $this->newUrl)) return null; if(preg_match_all("/\/$language\//", $this->oldUrl)) return $language; } return $this->defaultLanguage; } public static function getSubscribedEvents() { -
kunicmarko20 revised this gist
Jan 10, 2017 . 1 changed file with 12 additions and 32 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,7 @@ ////services: // app.locale.listener: // class: YourBundle\EventListener\LocaleListener // arguments: ["@router"] // tags: // - { name: kernel.event_subscriber } @@ -17,20 +17,15 @@ namespace YourBundle\EventListener; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\Routing\RouterInterface; use Symfony\Component\HttpKernel\Event\GetResponseEvent; use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\Routing\Matcher\UrlMatcher; use Symfony\Component\Routing\RequestContext; class LocaleListener implements EventSubscriberInterface { /** * @var routeCollection \Symfony\Component\Routing\RouteCollection @@ -42,32 +37,13 @@ class LocaleRedirectListener implements EventSubscriberInterface */ private $urlMatcher; private $oldUrl; private $newUrl; public function __construct(RouterInterface $router) { $this->routeCollection = $router->getRouteCollection(); $context = new RequestContext("/"); $this->urlMatcher = new UrlMatcher($this->routeCollection, $context); } @@ -83,25 +59,29 @@ public function onKernelRequest(GetResponseEvent $event) $this->newUrl = $request->getPathInfo(); $this->oldUrl = $request->headers->get('referer'); $locale = $this->checkLanguage(); if($locale === null) return; $request->setLocale($locale); $pathLocale = "/".$locale.$this->newUrl; //We have to catch the ResourceNotFoundException try { //Try to match the path with the local prefix $this->urlMatcher->match($pathLocale); $event->setResponse(new RedirectResponse($pathLocale)); } catch (\Symfony\Component\Routing\Exception\ResourceNotFoundException $e) { } catch (\Symfony\Component\Routing\Exception\MethodNotAllowedException $e) { } } private function checkLanguage(){ if(preg_match_all("/\/en\//", $this->newUrl)){ return null; } if(preg_match_all("/\/de\//", $this->newUrl)){ return null; } return $this->checkOldUrl(); } -
kunicmarko20 created this gist
Dec 15, 2016 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,118 @@ ////services: // app.locale.redirect: // class: YourBundle\EventListener\LocaleRedirectListener // arguments: ["@router", "%kernel.default_locale%"] // tags: // - { name: kernel.event_subscriber } // in default routing import your routing file with routes that have multilanguage //language_routing: // resource: "@YourBundle/Resources/config/multilanguage-routes.yml" // prefix: /{_locale} // requirements: // _locale: de|en // defaults: { _locale: de} <?php namespace YourBundle\EventListener; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\Routing\RouterInterface; use Symfony\Component\HttpKernel\Event\GetResponseEvent; use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\Routing\Matcher\UrlMatcher; use Symfony\Component\Routing\RequestContext; class LocaleRedirectListener implements EventSubscriberInterface { /** * @var Symfony\Component\Routing\RouterInterface */ private $router; /** * @var routeCollection \Symfony\Component\Routing\RouteCollection */ private $routeCollection; /** * @var urlMatcher \Symfony\Component\Routing\Matcher\UrlMatcher; */ private $urlMatcher; /** * @var string */ private $defaultLocale; /** * @var string */ private $localeRouteParam; /** * @var string */ private $oldUrl; /** * @var string */ private $newUrl; public function __construct(RouterInterface $router, $defaultLocale = 'de', $localeRouteParam = '_locale') { $this->router = $router; $this->routeCollection = $router->getRouteCollection(); $this->defaultLocale = $defaultLocale; $this->localeRouteParam = $localeRouteParam; $context = new RequestContext("/"); $this->urlMatcher = new UrlMatcher($this->routeCollection, $context); } public function onKernelRequest(GetResponseEvent $event) { //GOAL: // Redirect all incoming requests to their /locale/route equivalent when exists. // Do nothing if it already has /locale/ in the route to prevent redirect loops // Do nothing if the route requested has no locale param $request = $event->getRequest(); $this->newUrl = $request->getPathInfo(); $this->oldUrl = $request->headers->get('referer'); $locale = $this->checkLanguage(); $request->setLocale($locale); $pathLocale = "/".$locale.$this->newUrl; //We have to catch the ResourceNotFoundException try { //Try to match the path with the local prefix $this->urlMatcher->match($pathLocale); $event->setResponse(new RedirectResponse($pathLocale)); } catch (\Symfony\Component\Routing\Exception\ResourceNotFoundException $e) { } } private function checkLanguage(){ if(preg_match_all("/\/en\//", $this->newUrl)){ return 'en'; } if(preg_match_all("/\/de\//", $this->newUrl)){ return 'de'; } return $this->checkOldUrl(); } private function checkOldUrl(){ return preg_match_all("/\/en\//", $this->oldUrl) ? 'en' : 'de'; } public static function getSubscribedEvents() { return array( // must be registered before the default Locale listener KernelEvents::REQUEST => array(array('onKernelRequest', 17)), ); } }