I've been trying for days adding locale prefix to url in symfony2 application. Everything works fine, but when i try to login application redirects me to default language although i chose another language before log in (for example: I chose english before login in with "en" in url but after login it redirects me to homepage with default language and removes en from url)
In twig i change language like this:
<li><a href="{{ path(app.request.get('_route'),app.request.get('_route_params')|merge({'_locale':'en'})) }}">EN</a></li>
I'm using JMSI18nRoutingBundle and i have putted in config.yml this code:
jms_i18n_routing:
default_locale: "%locale%"
locales: [de, en, it, fr]
strategy: prefix_except_default
My security.yml looks like this.
security:
encoders:
HotelPlanBundle\Entity\User: bcrypt
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: ROLE_SUPER_ADMIN
providers:
fos_userbundle:
id: fos_user.user_provider.username_email
firewalls:
main:
pattern: ^/
form_login:
provider: fos_userbundle
login_path: fos_user_security_login
default_target_path: /checkrole
check_path: fos_user_security_check
use_referer: true
csrf_provider: security.csrf.token_manager
oauth:
resource_owners:
facebook: "/login/check-facebook"
google: "/login/check-google"
instagram: "/login/check-instagram"
login_path: fos_user_security_login
use_referer: true
default_target_path: /extraregistration
oauth_user_provider:
service: app.fos_user.oauth_provider
logout:
path: fos_user_security_logout
anonymous: true
access_control:
- { path: ^/login, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/connect/, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/administrator/, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: /administratordashboard, role: ROLE_SUPER_ADMIN }
- { path: ^/homepage, role: ROLE_ADMIN }
- { path: /listsinglevideo/, role: ROLE_SUPER_ADMIN }
- { path: /upload, role: ROLE_ADMIN }
- { path: ^/[^/]+/, role: IS_AUTHENTICATED_ANONYMOUSLY }enter
I also added LocaleListener.php
<?php
namespace HotelPlanBundle\Bundle\EventListener;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class LocaleListener implements EventSubscriberInterface
{
private $defaultLocale;
public function __construct($defaultLocale = 'de')
{
$this->defaultLocale = $defaultLocale;
}
public function onKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
if (!$request->hasPreviousSession()) {
return;
}
// try to see if the locale has been set as a _locale routing parameter
if ($locale = $request->attributes->get('_locale')) {
$request->getSession()->set('_locale', $locale);
} else {
// if no explicit locale has been set on this request, use one from the session
$request->setLocale($request->getSession()->get('_locale', $this->defaultLocale));
}
}
public static function getSubscribedEvents()
{
return array(
// must be registered before the default Locale listener
KernelEvents::REQUEST => array(array('onKernelRequest', 17)),
);
}
}
and in services.yml
services:
hotel.locale:
class: HotelPlanBundle\EventListener\LocaleListener
arguments: ["%kernel.default_locale%"]
tags:
- { name: kernel.event_subscriber }
I'm using FOSUSERbundle for login and registration and hwioauthbundle for facebook and twitter authentication. Same thing happens to me when I log in with facebook and twitter.
Any idea?
Thanks
Thanks to @R.CanserYanbakan the problem was in the security.ylm :default_target_path
firewalls:
main:
pattern: ^/
form_login:
provider: fos_userbundle
login_path: fos_user_security_login
default_target_path: /checkrole
i added the forward slash in the default_target_path it was redirected in the path directly not in the name of the path so instead we had to refer to the name of the path like this
firewalls:
main:
pattern: ^/
form_login:
provider: fos_userbundle
login_path: fos_user_security_login
default_target_path: checkrole //this is also the name of the route in the controller