Flag of Ukraine
SymfonyCasts stands united with the people of Ukraine

Updating security, translation & validator Recipes

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.

Start your All-Access Pass
Buy just this tutorial for $10.00

With a Subscription, click any sentence in the script to jump to that part of the video!

Login Subscribe

The composer recipes command tells us that we only have three more main Symfony recipes to update. Let's get to it! The next one is for security-bundle. Update it:

Updating symfony/securtity-bundle Recipe

composer recipes:install symfony/security-bundle --force -v

And then run:

git add -p

Woh! It looks like it made a lot of changes! But... like we've learned, what we're really seeing is it replacing all of our custom logic with the updated file from the recipe.

And... we want to keep pretty much all of our stuff: our custom encoder, user provider and firewall config. Let's look closely to see if there's anything interesting in the new code. Oh, there is one change: anonymous: true was changed to anonymous: lazy.

This is a new feature from Symfony 4.4. It basically means that, instead of Symfony figuring out who you're logged in as at the beginning of each request, it will do it lazily: it will wait until the moment your code tries to ask "who" is logged in. If your code never asks, then the authentication logic never runs. This was done to help make HTTP caching easier for pages that don't need any user info.

So, we do want this change. Hit "q" to exit this and... revert the changes with:

git checkout config/packages/security.yaml

Now, open that file in your editor, find anonymous and change it to lazy:

security:
... lines 2 - 16
firewalls:
... lines 18 - 20
main:
anonymous: lazy
... lines 23 - 61

Let's keep going:

git add -p

This time, say "y" to add the change... and "y" again for symfony.lock. Let's commit!

git commit -m "upgrading security recipe"

Done!

Upgrading the symfony/translation Recipe

What's next? Let's find out:

composer recipes

Ah, translation! Update it:

composer recipes:install symfony/translation --force -v

And walk through the changes:

git add -p

In translation.yaml, all the %locale% parameters were replaced with just en. The locale parameter is set in our config/services.yaml file:

... lines 1 - 5
parameters:
... line 7
locale: 'en'
... lines 9 - 51

This was originally added by a recipe.

So... what's going on here? Purely for simplification, instead of setting that parameter and then using it in this one file, the recipe was updated to remove the parameter and set the locale directly. You don't need to make this change if you don't want to.

But I'll say "y" and then "y" again for the symfony.lock file. Back in services.yaml, manually remove the locale parameter:

... lines 1 - 5
parameters:
... line 7
locale: 'en'
... lines 9 - 51

Why didn't the recipe remove that for me? Well, again, removing things - like old files or even old parameters - is not something the recipe update system currently handles.

Run:

git status

Then:

git add -p

And accept this one change. Commit!

git commit -m "updating translation recipe"

Updating the symfony/validator recipe

We're on a roll!

composer recipes

Oh, so close. Next is the validator recipe. Update it:

composer recipes:install symfony/validator --force -v

And walk through the changes:

git add -p

The first change is in config/packages/validator.yaml: it adds some new config that's commented out. This activates a new validation feature called auto-mapping. It's really cool - and we're going to talk about it later. Hit "y" to add these comments and... yep! This is the symfony.lock file. Press "y" again.

That was easy! Let's commit. Actually, I should have run git status, because this recipe also added a new file. We'll see it in a minute:

git commit -m "updating validator recipe"

Updating webpack-encore-bundle Recipe

Are we done? Run:

composer recipes

We are! Well, there is one more that starts with symfony/: webpack-encore-bundle. But that bundle isn't part of the main Symfony repository... so you can update it now or later. If you're interested, let's update it next. If you're not, skip ahead one chapter to start finding and fixing deprecations.

Leave a comment!

4
Login or Register to join the conversation
Jonathan P. Avatar
Jonathan P. Avatar Jonathan P. | posted 2 years ago

Hi !
Translating 'static' content seems easy enough, but how would one translate database fields (dynamic content) ? Any best practice / guidance ?

Reply
Maxim-M Avatar

Hi Antsen!

There is a page which answers your question.

However I'd love to see a tutorial about translations in general. It is especially interesting to see the best practice of working with {_locale} parameter in a route, when you have some JS front-end talking to endpoints on the server.

Reply

Hey Maxim,

Thank you for your interest in SymfonyCasts tutorials! Yeah, we do want to release a course about translations some day, but I have no release date yet. Meanwhile, I'd recommend you to look at this bundle: https://github.com/willdura... if you're interested in JS front-end translations in Symfony apps. :)

Cheers!

Reply

Hey @Antsen

There are Doctrine extensions that allows you to create translatable entities also some bundles which helps organising it. Unfortunately can't advice any guidance or best practice yet.

Cheers

Reply
Cat in space

"Houston: no signs of life"
Start the conversation!

What PHP libraries does this tutorial use?

// composer.json
{
    "require": {
        "php": "^7.3.0",
        "ext-iconv": "*",
        "antishov/doctrine-extensions-bundle": "^1.4", // v1.4.2
        "aws/aws-sdk-php": "^3.87", // 3.110.11
        "composer/package-versions-deprecated": "^1.11", // 1.11.99
        "doctrine/doctrine-bundle": "^2.0", // 2.0.6
        "doctrine/doctrine-migrations-bundle": "^1.3|^2.0", // 2.1.2
        "doctrine/orm": "^2.5.11", // v2.7.2
        "doctrine/persistence": "^1.3.7", // 1.3.8
        "easycorp/easy-log-handler": "^1.0", // v1.0.9
        "http-interop/http-factory-guzzle": "^1.0", // 1.0.0
        "knplabs/knp-markdown-bundle": "^1.7", // 1.8.1
        "knplabs/knp-paginator-bundle": "^5.0", // v5.0.0
        "knplabs/knp-snappy-bundle": "^1.6", // v1.7.0
        "knplabs/knp-time-bundle": "^1.8", // v1.11.0
        "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.3.0
        "nexylan/slack-bundle": "^2.1", // v2.2.1
        "oneup/flysystem-bundle": "^3.0", // 3.3.0
        "php-http/guzzle6-adapter": "^2.0", // v2.0.1
        "sensio/framework-extra-bundle": "^5.1", // v5.5.3
        "symfony/asset": "5.0.*", // v5.0.2
        "symfony/console": "5.0.*", // v5.0.2
        "symfony/dotenv": "5.0.*", // v5.0.2
        "symfony/flex": "^1.0", // v1.17.6
        "symfony/form": "5.0.*", // v5.0.2
        "symfony/framework-bundle": "5.0.*", // v5.0.2
        "symfony/mailer": "5.0.*", // v5.0.2
        "symfony/messenger": "5.0.*", // v5.0.2
        "symfony/monolog-bundle": "^3.5", // v3.5.0
        "symfony/security-bundle": "5.0.*", // v5.0.2
        "symfony/sendgrid-mailer": "5.0.*", // v5.0.2
        "symfony/serializer-pack": "^1.0", // v1.0.2
        "symfony/twig-bundle": "5.0.*", // v5.0.2
        "symfony/twig-pack": "^1.0", // v1.0.0
        "symfony/validator": "5.0.*", // v5.0.2
        "symfony/webpack-encore-bundle": "^1.4", // v1.7.2
        "symfony/yaml": "5.0.*", // v5.0.2
        "twig/cssinliner-extra": "^2.12", // v2.12.0
        "twig/extensions": "^1.5", // v1.5.4
        "twig/inky-extra": "^2.12" // v2.12.0
    },
    "require-dev": {
        "doctrine/doctrine-fixtures-bundle": "^3.0", // 3.3.0
        "fzaninotto/faker": "^1.7", // v1.8.0
        "symfony/browser-kit": "5.0.*", // v5.0.2
        "symfony/debug-bundle": "5.0.*", // v5.0.2
        "symfony/maker-bundle": "^1.0", // v1.14.3
        "symfony/phpunit-bridge": "5.0.*", // v5.0.2
        "symfony/profiler-pack": "^1.0", // v1.0.4
        "symfony/var-dumper": "5.0.*" // v5.0.2
    }
}
userVoice