How to manually set router locale in Symfony 2.8
I am running a Symfony 2.8
based web app which uses FOSUserBundle
to handle users.
The FOSUserBundle
routes are imported using the current locale as prefix:
// app/config/routing.yml
...
# fos_userbundle
user:
resource: "@FOSUserBundle/Resources/config/routing/all.xml"
prefix: "/_locale/user"
This works fine. If the users creates a new account using the web browser, the e-mail confirmation link is created the current locale, e.g. https://example.com/XX/user/register/confirm/TOKEN
Now I have added a REST API
which allows to create new users from within the mobile Apps (iOS and Android). This is done by simple POSTing the necessary data (username, password, etc.) including the locale to controller which than handles the user creation.
The problem: Users created using the REST API
always receive confirmation links using the default locale, which is en
.
While setting the locale manually works fine to generate the confirmation mail in the correct language, it has no effect on the confirmation link:
public function registerAction(Request $request)
$username = getDataFromRequest(...);
$userLocale = getDateFromRequest(...);
...
// Set locale manually
$translator->setLocale(userLocale);
$sessionLocale = $request->getSession()->set('_locale', userLocale);
$request->setLocale(userLocale);
...
$mailer = $this->get('fos_user.mailer');
$tokenGenerator = $this->get('fos_user.util.token_generator');
if (null === $user->getConfirmationToken())
$user->setConfirmationToken($tokenGenerator->generateToken());
$mailer->sendConfirmationEmailMessage($user);
...
I have checked the code of sendConfirmationEmailMessage(...)
and it uses the default router to generate the confirmation link:
$url = $this->router->generate('fos_user_registration_confirm', array('token' => $user->getConfirmationToken()), UrlGeneratorInterface::ABSOLUTE_URL);
Question: How to tell the router to use the correct locale?
symfony locale fosuserbundle symfony-routing
add a comment |
I am running a Symfony 2.8
based web app which uses FOSUserBundle
to handle users.
The FOSUserBundle
routes are imported using the current locale as prefix:
// app/config/routing.yml
...
# fos_userbundle
user:
resource: "@FOSUserBundle/Resources/config/routing/all.xml"
prefix: "/_locale/user"
This works fine. If the users creates a new account using the web browser, the e-mail confirmation link is created the current locale, e.g. https://example.com/XX/user/register/confirm/TOKEN
Now I have added a REST API
which allows to create new users from within the mobile Apps (iOS and Android). This is done by simple POSTing the necessary data (username, password, etc.) including the locale to controller which than handles the user creation.
The problem: Users created using the REST API
always receive confirmation links using the default locale, which is en
.
While setting the locale manually works fine to generate the confirmation mail in the correct language, it has no effect on the confirmation link:
public function registerAction(Request $request)
$username = getDataFromRequest(...);
$userLocale = getDateFromRequest(...);
...
// Set locale manually
$translator->setLocale(userLocale);
$sessionLocale = $request->getSession()->set('_locale', userLocale);
$request->setLocale(userLocale);
...
$mailer = $this->get('fos_user.mailer');
$tokenGenerator = $this->get('fos_user.util.token_generator');
if (null === $user->getConfirmationToken())
$user->setConfirmationToken($tokenGenerator->generateToken());
$mailer->sendConfirmationEmailMessage($user);
...
I have checked the code of sendConfirmationEmailMessage(...)
and it uses the default router to generate the confirmation link:
$url = $this->router->generate('fos_user_registration_confirm', array('token' => $user->getConfirmationToken()), UrlGeneratorInterface::ABSOLUTE_URL);
Question: How to tell the router to use the correct locale?
symfony locale fosuserbundle symfony-routing
add a comment |
I am running a Symfony 2.8
based web app which uses FOSUserBundle
to handle users.
The FOSUserBundle
routes are imported using the current locale as prefix:
// app/config/routing.yml
...
# fos_userbundle
user:
resource: "@FOSUserBundle/Resources/config/routing/all.xml"
prefix: "/_locale/user"
This works fine. If the users creates a new account using the web browser, the e-mail confirmation link is created the current locale, e.g. https://example.com/XX/user/register/confirm/TOKEN
Now I have added a REST API
which allows to create new users from within the mobile Apps (iOS and Android). This is done by simple POSTing the necessary data (username, password, etc.) including the locale to controller which than handles the user creation.
The problem: Users created using the REST API
always receive confirmation links using the default locale, which is en
.
While setting the locale manually works fine to generate the confirmation mail in the correct language, it has no effect on the confirmation link:
public function registerAction(Request $request)
$username = getDataFromRequest(...);
$userLocale = getDateFromRequest(...);
...
// Set locale manually
$translator->setLocale(userLocale);
$sessionLocale = $request->getSession()->set('_locale', userLocale);
$request->setLocale(userLocale);
...
$mailer = $this->get('fos_user.mailer');
$tokenGenerator = $this->get('fos_user.util.token_generator');
if (null === $user->getConfirmationToken())
$user->setConfirmationToken($tokenGenerator->generateToken());
$mailer->sendConfirmationEmailMessage($user);
...
I have checked the code of sendConfirmationEmailMessage(...)
and it uses the default router to generate the confirmation link:
$url = $this->router->generate('fos_user_registration_confirm', array('token' => $user->getConfirmationToken()), UrlGeneratorInterface::ABSOLUTE_URL);
Question: How to tell the router to use the correct locale?
symfony locale fosuserbundle symfony-routing
I am running a Symfony 2.8
based web app which uses FOSUserBundle
to handle users.
The FOSUserBundle
routes are imported using the current locale as prefix:
// app/config/routing.yml
...
# fos_userbundle
user:
resource: "@FOSUserBundle/Resources/config/routing/all.xml"
prefix: "/_locale/user"
This works fine. If the users creates a new account using the web browser, the e-mail confirmation link is created the current locale, e.g. https://example.com/XX/user/register/confirm/TOKEN
Now I have added a REST API
which allows to create new users from within the mobile Apps (iOS and Android). This is done by simple POSTing the necessary data (username, password, etc.) including the locale to controller which than handles the user creation.
The problem: Users created using the REST API
always receive confirmation links using the default locale, which is en
.
While setting the locale manually works fine to generate the confirmation mail in the correct language, it has no effect on the confirmation link:
public function registerAction(Request $request)
$username = getDataFromRequest(...);
$userLocale = getDateFromRequest(...);
...
// Set locale manually
$translator->setLocale(userLocale);
$sessionLocale = $request->getSession()->set('_locale', userLocale);
$request->setLocale(userLocale);
...
$mailer = $this->get('fos_user.mailer');
$tokenGenerator = $this->get('fos_user.util.token_generator');
if (null === $user->getConfirmationToken())
$user->setConfirmationToken($tokenGenerator->generateToken());
$mailer->sendConfirmationEmailMessage($user);
...
I have checked the code of sendConfirmationEmailMessage(...)
and it uses the default router to generate the confirmation link:
$url = $this->router->generate('fos_user_registration_confirm', array('token' => $user->getConfirmationToken()), UrlGeneratorInterface::ABSOLUTE_URL);
Question: How to tell the router to use the correct locale?
symfony locale fosuserbundle symfony-routing
symfony locale fosuserbundle symfony-routing
asked Nov 14 '18 at 15:44
Andrei HerfordAndrei Herford
5,8591048108
5,8591048108
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Define the locale as a parameter of the route generator :
$url = $this->router->generate(
'fos_user_registration_confirm',
array(
'token' => $user->getConfirmationToken(),
'_locale' => $userLocale,
),
UrlGeneratorInterface::ABSOLUTE_URL
);
That would be possible but I would have to override / re-implement theFOSUserBundle
methodsendConfirmationEmailMessage(...)
to achieve this. Possible but quite complicated. I would prefer a solution within my own code without the need to touch any third party code. I do not understand where the router gets the wrong locale from in the first place.
– Andrei Herford
Nov 15 '18 at 6:58
add a comment |
After digging through half the Symfony code I found out from where the router gets the locale and found a solution that works for me. Maybe it is useful for others as well.
Simply update the router locale using the following code:
$router->getContext()->setParameter('_locale', $locale);
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53303898%2fhow-to-manually-set-router-locale-in-symfony-2-8%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Define the locale as a parameter of the route generator :
$url = $this->router->generate(
'fos_user_registration_confirm',
array(
'token' => $user->getConfirmationToken(),
'_locale' => $userLocale,
),
UrlGeneratorInterface::ABSOLUTE_URL
);
That would be possible but I would have to override / re-implement theFOSUserBundle
methodsendConfirmationEmailMessage(...)
to achieve this. Possible but quite complicated. I would prefer a solution within my own code without the need to touch any third party code. I do not understand where the router gets the wrong locale from in the first place.
– Andrei Herford
Nov 15 '18 at 6:58
add a comment |
Define the locale as a parameter of the route generator :
$url = $this->router->generate(
'fos_user_registration_confirm',
array(
'token' => $user->getConfirmationToken(),
'_locale' => $userLocale,
),
UrlGeneratorInterface::ABSOLUTE_URL
);
That would be possible but I would have to override / re-implement theFOSUserBundle
methodsendConfirmationEmailMessage(...)
to achieve this. Possible but quite complicated. I would prefer a solution within my own code without the need to touch any third party code. I do not understand where the router gets the wrong locale from in the first place.
– Andrei Herford
Nov 15 '18 at 6:58
add a comment |
Define the locale as a parameter of the route generator :
$url = $this->router->generate(
'fos_user_registration_confirm',
array(
'token' => $user->getConfirmationToken(),
'_locale' => $userLocale,
),
UrlGeneratorInterface::ABSOLUTE_URL
);
Define the locale as a parameter of the route generator :
$url = $this->router->generate(
'fos_user_registration_confirm',
array(
'token' => $user->getConfirmationToken(),
'_locale' => $userLocale,
),
UrlGeneratorInterface::ABSOLUTE_URL
);
answered Nov 14 '18 at 16:00
Guillaume SGuillaume S
435
435
That would be possible but I would have to override / re-implement theFOSUserBundle
methodsendConfirmationEmailMessage(...)
to achieve this. Possible but quite complicated. I would prefer a solution within my own code without the need to touch any third party code. I do not understand where the router gets the wrong locale from in the first place.
– Andrei Herford
Nov 15 '18 at 6:58
add a comment |
That would be possible but I would have to override / re-implement theFOSUserBundle
methodsendConfirmationEmailMessage(...)
to achieve this. Possible but quite complicated. I would prefer a solution within my own code without the need to touch any third party code. I do not understand where the router gets the wrong locale from in the first place.
– Andrei Herford
Nov 15 '18 at 6:58
That would be possible but I would have to override / re-implement the
FOSUserBundle
method sendConfirmationEmailMessage(...)
to achieve this. Possible but quite complicated. I would prefer a solution within my own code without the need to touch any third party code. I do not understand where the router gets the wrong locale from in the first place.– Andrei Herford
Nov 15 '18 at 6:58
That would be possible but I would have to override / re-implement the
FOSUserBundle
method sendConfirmationEmailMessage(...)
to achieve this. Possible but quite complicated. I would prefer a solution within my own code without the need to touch any third party code. I do not understand where the router gets the wrong locale from in the first place.– Andrei Herford
Nov 15 '18 at 6:58
add a comment |
After digging through half the Symfony code I found out from where the router gets the locale and found a solution that works for me. Maybe it is useful for others as well.
Simply update the router locale using the following code:
$router->getContext()->setParameter('_locale', $locale);
add a comment |
After digging through half the Symfony code I found out from where the router gets the locale and found a solution that works for me. Maybe it is useful for others as well.
Simply update the router locale using the following code:
$router->getContext()->setParameter('_locale', $locale);
add a comment |
After digging through half the Symfony code I found out from where the router gets the locale and found a solution that works for me. Maybe it is useful for others as well.
Simply update the router locale using the following code:
$router->getContext()->setParameter('_locale', $locale);
After digging through half the Symfony code I found out from where the router gets the locale and found a solution that works for me. Maybe it is useful for others as well.
Simply update the router locale using the following code:
$router->getContext()->setParameter('_locale', $locale);
answered Nov 20 '18 at 13:39
Andrei HerfordAndrei Herford
5,8591048108
5,8591048108
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53303898%2fhow-to-manually-set-router-locale-in-symfony-2-8%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown