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.
Asked
Active
Viewed 297 times
-1
-
Perhaps [gethostname](https://www.php.net/manual/en/function.gethostname.php) – Cerad Oct 07 '21 at 14:27
-
1Does this answer your question? [Symfony 5 Generating a URL from Console Command](https://stackoverflow.com/questions/62283956/symfony-5-generating-a-url-from-console-command) – yivi Oct 07 '21 at 15:59
-
What do you mean by "generate"? Either you know which value to use (then set it), or you need another approach to determine it – Nico Haase Oct 09 '21 at 15:41
1 Answers
0
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

V-Light
- 3,065
- 3
- 25
- 31