Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Discussion options

Symfony 5.4: works
$builder = self::getContainer()->get(EndPointBuilder::class);
Symfony 6.0.3: fail
$builder = self::getContainer()->get(EndPointBuilder::class);

Error:
Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException: The "App\Service\CableLengthCalculator\Calculator" 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.

Did I miss something in the upgrade instructions?

You must be logged in to vote

Replies: 5 comments · 1 reply

Comment options

I think you need to make your service public:
https://symfony.com/doc/current/service_container/alias_private.html

And some info about injecting services, but i think you know that.
Read news first (second part):
https://symfony.com/blog/new-in-symfony-5-4-controller-changes
And then read current docs (pass services to constructor or controller function as argument)
https://symfony.com/doc/current/service_container.html

You must be logged in to vote
0 replies
Comment options

@alessandro-podo Follow the instructions here: https://symfony.com/doc/current/testing.html#retrieving-services-in-the-test In particular you should be using static::getContainer instead of self::getContainer. And you might need to add your service to config/services_test.yaml.

You must be logged in to vote
0 replies
Comment options

Thanks for the answers.
After I set the services public, it works.

But I just wonder why it changed with the update to 6.0.

You must be logged in to vote
0 replies
Comment options

After some trial and error, I found the solution.
The TestContainer contains only the services that are loaded somewhere else via the constructor. Then you don't have to set them to public.

is this with known behavior?

You must be logged in to vote
0 replies
Comment options

It's because all services private by default. See what is private services. You can't get private service from container like this

$container->get(MyService::class); // <-- fail because it's private except if you describe this services as public in services.yaml

For test you need services_test.yaml which will configure and make your services accessable by container->get(...)

services:
  _defaults:
    public: true # <-- this means that all services described in this file will be public by default and will be accessable by container->get

  App\Factory\UserFactory: ~ # <-- will be accessable
You must be logged in to vote
1 reply
@vince83110
Comment options

Do you really need the service as public if you make it accessible ? Or the opposite by the way ?

From doc:
The container from static::getContainer() is actually a special test container. It gives you access to both the public services and the non-removed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
🙏
Q&A
Labels
None yet
5 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.