Formatting service throws non-public error

Not sure if this is a bug or if I’m just doing something wrong, but when I attempt to format currency using \Pimcore\Localization\IntlFormatter as documented here: https://pimcore.com/docs/6.x/Development_Documentation/Multi_Language_i18n/Formatting_Service.html in my controller, I get an error message:

The "Pimcore\Localization\IntlFormatter" service or alias has been removed or inlined when the container was compiled. You should either make it public, or stop using the container directly and use dependency injection instead.

I can get around it by setting in my services.yml:

Pimcore\Localization\IntlFormatter:
    public: true

I can’t imagine that’s the desired solution, however. Is there a better solution?

Hi there.

There is, indeed a solution, and you’re getting it in the error message.

The service itself is not public, which means that it shouldn’t be accessed via container.

The container actually compiles all of the public services and initializes them. The private services should be accessed via dependency injection (lazy loaded).

Since all of the Controllers are already public, if your autowiring is on, you should just insert the intlFormatter service as an argument in your constructor. Then everything should work properly.

If you’re using the service within a service, then just place it in the arguments list of your service and, again, everything should work.

This way, your container doesn’t do any lifting regarding the service, you just access it on a case-by-case basis.

Hope this helps.

That indeed does help, and it works great. Perhaps, that’s something that should be changed in the documentation for v6?