I found an easy way of adding a locale to my Symfony routes, as described here: Locale switch in login of FOSUserBundle
Some relevant settings in my project:
config
framework:
translator: { fallback: "%locale%" }
default_locale: nl
services
fosmailer: #fos_user.mailer.twig_swift
routing
fos_user_register:
resource: "#FOSUserBundle/Resources/config/routing/registration.xml"
prefix: /{_locale}/registreren
I'm trying to get FOSUser to send a confirmation mail in the newly created users' browserlanguage.
For this I'm saving the locate found by javascript in my database.
Then, I'm setting my request to that locale and send the confirmation mail manually like this:
$request->setLocale($locale); //($locale = 'en_US')
$mailer = $this->get('fosmailer');
$mailer->sendConfirmationEmailMessage($user);
My email template contains a bunch of {{ 'foobar' | trans}} tags, which work perfectly fine. But I'm also using the {{confirmationUrl}} which is generated by FOSUser.
In an English mail this confirmation url will still show up as:
/nl/registreren/confirm/1234..
Any ideas why my mail is localized but my url isn't?
The URL address is not translated, because localization of URL addresses is not part of standard Symfony2 installation.
In order to localize URL addresses you need to use some bundle which solves this issue, for example BeSimpleI18nRoutingBundle or you can create your own solution.
Related
I want to write a script that send notification mails to users and execute it every hour and I want it to match the server name that host the symfony site. I've already tried with $_SERVER but it's not defined and the solution on this post but there is no request with the console so it's does work.
Since there is no traditional 'web request/response' thing in console command, all URLs you generate with {{ path('...') }} in twig or $this->generateUrl('...') or similar controllers/services´, will be probablyhttp://localhost/...
To fix that, simply add: (if you're on symfony 5.1+)
# config/packages/routing.yaml
framework:
router:
# but you should move it to your .env file and user two differen env-variables for host and scheme
default_uri: 'https://you-site-hostname.com/'
or if you are using older version, put
router.request_context.scheme and router.request_context.host to you parameters. Like
# config/services.yaml
parameters:
router.request_context.scheme: 'https'
asset.request_context.host: 'you-site-hostname.com'
Take a look here: https://symfony.com/doc/current/routing.html#generating-urls-in-commands
And here (my personal advice) https://symfonycasts.com/screencast/mailer/route-context
I have a simple form to login and reminder password, I overwrite the FOSUser template and everything works fine, but when I want to email the password reminder I do not get it to the mail.
This looks like a swiftmailer and fos configuration in config.yml:
swiftmailer:
transport: '%mailer_transport%'
host: '%mailer_host%'
username: '%mailer_user%'
password: '%mailer_password%'
spool: { type: memory }
fos_user:
db_driver: orm
firewall_name: main
user_class: AirblogBundle\Entity\User
from_email:
address: "admin#admin.com"
sender_name: "Admin"
resetting:
email:
from_email:
address: noreply#yoursite.com
sender_name: No Reply
token_ttl: 0
After sending the email from the resetting/request path, I get the following information:
An email has been sent to ...#wp.pl. It contains a link you must click to reset your password.
Which seems correct because the address entered in the form has the same ending, but my mailbox does not receive any message.
ps: One more question, how to simply overwrite the Twig template, so the login and reminder of the hymn was on one page? Do you need to overwrite the controller?
Did you configure the needed parameters of SwiftMailerBundle well ?
First of all I suggest you to use Symfony's command to check your swift mailer configuration.
Specify the good arguments to send you a debug mail with reading the help command
php app/console swiftmailer:email:send --help
You should receive a mail on the email address you wrote in command.
After this step you can assume that your mail configuration is good
Reset your forget password request
One important (in the version installed on my project, composer requirement ~2.0) thing to know that is FOSUserBundle don't resend mail if you didn't click on the previous reset link sended BUT he display to you the message that he sended the mail.
If you want to "reset" your password request for a specific user
UPDATE user SET confirmation_token = NULL, password_requested_at = NULL WHERE username = "yourdebugusername"
Extending FOSUserBundle Twig templates
You should read this documentation
Basically you can override all the FOSUserTemplate under vendor/friendsofsymfony/user-bundle/Resources/views
I hope it's help !
For overriding FOSUser twig template looks the Symfony Doc
Where do I find the route name for logging in with facebook? I don't want to hardcode the routes into the twig templates.
Short answer
Route name is hwi_oauth_service_redirect:
Name Method Scheme Host Path
hwi_oauth_service_redirect ANY ANY ANY /connect/{service}
So, for facebook you can generate connect route in view via:
{{ path('hwi_oauth_service_redirect', { service: 'facebook' }) }}
How to debug similar problems
I was also amazed how HWIOAuthBundle doesn't mention route name in documentation anywhere, or at least it isn't obviously stated and I couldn't find it.
When you have this kind of problem, you can use $ app/console router:debug command, which will provide you dump of all routes defined.
I am applying this tutorial into symfony 2.4, I've finished the setup in the config.yml and everything, I managed to visit the admin/google/analytics page, but the problem is when I tried to authenticate with the parameters I've created in the config.yml file, it is searching for the scope, here is the parameters.
happy_r_google_analytics:
host: www.example.com
profile_id: MyProfileId
tracker_id: UA-TRACKER-ID
token_file_path: %kernel.root_dir%/var/storage
happy_r_google_api:
application_name: Project Default Service Account
oauth2_client_id: OAuthClientID
oauth2_client_secret: OAuthClientSecret
oauth2_redirect_uri: http://www.example.com/app_local.php/admin/google/analytics/oauth2callback
developer_key: DevelopperKey
site_name: http://www.example.com
I think there's no problem here, I've got no idea where I can set the scope so the google Api client can set it to https://www.googleapis.com/auth/analytics.readonly
You need to define a scope. If you use Google Auth, check Authorization scopes for it.
You must do something like:
$googleClient = new \Google_Client();
$googleClient->setScopes(array(
'https://www.googleapis.com/auth/plus.me',
'https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/userinfo.profile',
));
I'm using FosUserBundle with Symfony2.1 and when I try to connect a user that is not enabled, the following error is displayed on my form : User account is disabled, which is great but it is a French website and I would like to translate this message in french.
I've already translated session messages and validators in translations folder : messages.fr.yml and validators.fr.yml and MyUserBundle.fr.yml but I can't get the ones created by the SecurityContext.
All messages from FOSUserBundle are using "FOSUserBundle" as the message domain. So you have to put your translation into ./app/Resources/translations/FOSUserBundle.fr.yml.
If you've derived your own bundle based on FOSUserBundle the file goes into ./src/YourUserBundle/Resources/translations/FOSUserBundle.fr.yml.