Chapters
31 Chapters
|
3:19:40
|
Login to bookmark this video
-
Course Code
Subscribe to download the code!Compatible PHP versions: ^7.1.3
Subscribe to download the code!Compatible PHP versions: ^7.1.3
-
This Video
Subscribe to download the video!
Subscribe to download the video!
-
Subtitles
Subscribe to download the subtitles!
Subscribe to download the subtitles!
-
Course Script
Subscribe to download the script!
Subscribe to download the script!
14.
Router Request Context: Fix Paths in the CLI
Scroll down to the script below, click on any sentence (including terminal blocks) to jump to that spot in the video!
Subscribe to jump to this part in the video!
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
This tutorial is built on Symfony 4.3, but will work well with Symfony 4.4 or 5.
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": "^7.1.3",
"ext-iconv": "*",
"aws/aws-sdk-php": "^3.87", // 3.110.11
"composer/package-versions-deprecated": "^1.11", // 1.11.99
"doctrine/annotations": "^1.0", // 1.10.1
"doctrine/doctrine-bundle": "^1.6.10", // 1.11.2
"doctrine/doctrine-migrations-bundle": "^1.3|^2.0", // v2.0.0
"doctrine/orm": "^2.5.11", // v2.7.2
"knplabs/knp-markdown-bundle": "^1.7", // 1.7.1
"knplabs/knp-paginator-bundle": "^2.7", // v2.8.0
"knplabs/knp-snappy-bundle": "^1.6", // v1.6.0
"knplabs/knp-time-bundle": "^1.8", // v1.9.1
"league/flysystem-aws-s3-v3": "^1.0", // 1.0.23
"league/flysystem-cached-adapter": "^1.0", // 1.0.9
"league/html-to-markdown": "^4.8", // 4.8.2
"liip/imagine-bundle": "^2.1", // 2.1.0
"nexylan/slack-bundle": "^2.1,<2.2.0", // v2.1.0
"oneup/flysystem-bundle": "^3.0", // 3.1.0
"php-http/guzzle6-adapter": "^1.1", // v1.1.1
"phpdocumentor/reflection-docblock": "^3.0|^4.0", // 4.3.1
"sensio/framework-extra-bundle": "^5.1", // v5.4.1
"stof/doctrine-extensions-bundle": "^1.3", // v1.3.0
"symfony/asset": "^4.0", // v4.3.4
"symfony/console": "^4.0", // v4.3.4
"symfony/flex": "^1.9", // v1.21.6
"symfony/form": "^4.0", // v4.3.4
"symfony/framework-bundle": "^4.0", // v4.3.4
"symfony/mailer": "4.3.*", // v4.3.4
"symfony/messenger": "4.3.*", // v4.3.4
"symfony/property-access": "4.3.*", // v4.3.4
"symfony/property-info": "4.3.*", // v4.3.4
"symfony/security-bundle": "^4.0", // v4.3.4
"symfony/sendgrid-mailer": "4.3.*", // v4.3.4
"symfony/serializer": "4.3.*", // v4.3.4
"symfony/twig-bundle": "^4.0", // v4.3.4
"symfony/validator": "^4.0", // v4.3.4
"symfony/web-server-bundle": "^4.0", // v4.3.4
"symfony/webpack-encore-bundle": "^1.4", // v1.6.2
"symfony/yaml": "^4.0", // v4.3.4
"twig/cssinliner-extra": "^2.12", // v2.12.0
"twig/extensions": "^1.5", // v1.5.4
"twig/extra-bundle": "^2.12|^3.0", // v2.12.1
"twig/inky-extra": "^2.12", // v2.12.0
"twig/twig": "^2.12|^3.0" // v2.13.1
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.0", // 3.2.2
"easycorp/easy-log-handler": "^1.0.2", // v1.0.7
"fzaninotto/faker": "^1.7", // v1.8.0
"symfony/browser-kit": "4.3.*", // v4.3.5
"symfony/debug-bundle": "^3.3|^4.0", // v4.3.4
"symfony/dotenv": "^4.0", // v4.3.4
"symfony/maker-bundle": "^1.0", // v1.13.0
"symfony/monolog-bundle": "^3.0", // v3.4.0
"symfony/phpunit-bridge": "^3.3|^4.0", // v4.3.4
"symfony/stopwatch": "4.3.*", // v4.3.4
"symfony/var-dumper": "^3.3|^4.0", // v4.3.4
"symfony/web-profiler-bundle": "4.3.*" // v4.3.4
}
}
6 Comments
I'd like to share a rare issue when sending async e-mails using the url twig function.
I always get the error: Unable to generate a URL for the named route "THE_ROUTE_NAME" as such route does not exist.
The project I'm working on has all routes prefixed with /en-en, like this:
And I also have in the parameters file (where SITE_BASE_SCHEME and SITE_BASE_HOST are defined in a .env file):
For this error to not be thrown I have to explicitly pass to the twig url function the _locale parameter, for example: url('frontend_legal_terms', {'_locale': locale})
I wonder if there is a better way to use the url twig function without passing the _locale parameter explicitly.
Many thanks in advance!
Hey Samuel,
Ah, yes, I see, it sounds like an edge case. Thanks for mention it! I think the problem is that there's no locale in the request that is handled I suppose by CRON in your case? So yes, request context helps with host and scheme as you set it explicitly via parameters, but it does not help with locale. Hm, but do you have a default locale set? If the locale wasn't specified explicitly, it should use default locale. I'd suppose it should work out of the box, but probably it's not.
Unfortunately I don't see a specific parameter name that would help with injecting locale, but I see that Symfony\Component\Routing\RequestContext service has setQueryString() method that should work for this, and I think you can create a listener that will inject a locale to that RequestContext. I suppose you can do it for ConsoleEvents::COMMAND event and probably for WorkerMessageReceivedEvent::class one to make it work with Cron.
At least, you can try to check it - add that listener for command, generate a URL and "dump(); die;" it.
I hope this helps!
Cheers!
Hi Victor,
First of all thank you for your help, very helpful indeed.
The scenario is a supervisor but in the essence is like a cron, you are right :)
Setting in the configuration file framework => default_locale parameter does not work in this case.
I played around with your idea about WorkerMessageReceivedEvent. It has a lot of sense, because any message to be handled will go through it.
Setting the _locale to the RequestContext instance works fine :) !!!
This helps not passing the _locale parameter to every twig url function call, which allows working with twig templates as a default web request. This small big detail helps working the team smoothly and without unexpected errors :)
In a short period of time we will be translating the website I am working on, so it will be a good moment for refactoring and for considering the same approach for the translator instance (in this case translator->setLocale), so translating a text like 'text-to-be-translated' | trans, will be shorter than writing every time 'text-to-be-translated' | trans([], 'DOMAIN', 'locale')
Again, thank you very much indeed for your help and I hope this helps to the community.
Regards! :)
Hey Samuel,
Thank you for your feedback and confirming that it did the trick for you! I'm really happy :)
Cheers!
In Symfony 5.1, the
default_urioption was introduced (https://symfony.com/doc/current/routing.html#generating-urls-in-commands). Is this a valid replacement for the scheme and host parameters used in this tutorial?Hey Evelyn P.
Yep it IS, feel free to try it =)
Cheers!
"Houston: no signs of life"
Start the conversation!