Closed Barno closed 4 years ago
I found one solution, is not the Best Solution but works
in my homepage
HomepageController
public function indexAction() { $request = $this->get('request'); /** * Store it into session the user language */ $sessionId = $this->get("session"); if($sessionId->get("lingua")==""){ $this->get('session')->set('lingua', $request->getLocale()); } return array(); }
in the other bundle of FOSUserBundle that have been overwritten
UserBundle/Controller/RegistrationController.php (for example)
public function customAction() { /** * Store it into session the user language */ $sessionId = $this->container->get("session"); if($sessionId->get("lingua")==""){ $this->container->get("session")->set('lingua', $this->container->get('request')->getLocale()); } return array( ); }
Then i create a service like: http://symfony.com/doc/current/cookbook/session/locale_sticky_session.html
services.xml
services: acme_locale.locale_listener: class: Acme\LocaleBundle\EventListener\LocaleListener arguments: ["%kernel.default_locale%",@session] tags: - { name: kernel.event_subscriber }
LocaleListener
namespace Acme\LocaleBundle\EventListener; use Symfony\Component\HttpKernel\Event\GetResponseEvent; use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpFoundation\Session; class SnLocaleListener implements EventSubscriberInterface { private $defaultLocale; private $session; public function __construct($defaultLocale = 'it', $session) { $this->defaultLocale = $defaultLocale; $this->session = $session; } public function onKernelRequest(GetResponseEvent $event) { $request = $event->getRequest(); if (!$request->hasPreviousSession()) { return; } //incapsula la lingua in sessione se esiste $locale_session = $this->session->get('lingua'); if ($locale_session == "") { // prova a vedere se il locale sia stato impostato come parametro _locale di una rotta if ($locale = $request->attributes->get('_locale')) { $request->getSession()->set('_locale', $locale); } else { // se non trova un locale esplicito in questa richiesta, usa quello della sessione $request->setLocale($request->getSession()->get('_locale', $this->defaultLocale)); } } else { $request->setLocale($request->getSession()->get('_locale', $locale_session)); $request->getSession()->set('_locale', $locale_session); $request->attributes->set('_locale', $locale_session); } } public static function getSubscribedEvents() { return array( // deve essere registrato prima dell'ascoltatore predefinito di locale KernelEvents::REQUEST => array(array('onKernelRequest', 17)), ); } }
/app/config/security.yml
.. oauth: remember_me: true resource_owners: facebook: "/%locale%/login/check-facebook" twitter: "/%locale%/login/check-twitter" login_path: fos_user_security_ripeti_login failure_path: fos_user_security_ripeti_login ..
sorry for my english is not perfect .. This solution for me works, It will not be the best
/%locale%/login/check-facebook
is equivalent to writing /it/login/check-facebook
, because %locale%
will be replaced by the DIC parameter locale
. If you want a more dynamic path (using the request locale), you need to use a route name in the config, not a path.
thanks for reply, if i use
twitter_login
it__RG__twitter_login ANY ANY ANY /it/login/check-twitter en__RG__twitter_login ANY ANY ANY /en/login/check-twitter
i have this error
No resource owner with name 'check-twitter'
after the redirect from twitter
When using i18n, you can avoid getting the "no resource owner" error by disabling i18n on hwi routing:
#HWIOAuthBundle routes hwi_oauth_security: resource: "@HWIOAuthBundle/Resources/config/routing/login.xml" prefix: /login hwi_oauth_connect: resource: "@HWIOAuthBundle/Resources/config/routing/connect.xml" prefix: /login hwi_oauth_redirect: resource: "@HWIOAuthBundle/Resources/config/routing/redirect.xml" prefix: /login facebook_login: pattern: /login/check-facebook options: { i18n: false } google_login: pattern: /login/check-google options: { i18n: false } twitter_login: pattern: /login/check-twitter options: { i18n: false }
^^
hi, i have a problem with check-facebook and check-twitter
i use also this bundle:JMSI18nRoutingBundle and this https://gist.github.com/danvbe/4476697
my routes are:
/app/config/routing.yml
/app/config/config.yml
/app/config/security.yml
when i try to login with facebook or twitter i have this error:
if i do this:
works. if I am visiting my application in English (/en) redirects back with my /it (im italian)
/app/config/parameters.yml