Deprecated Environment class

Adapter
Member | 3
+
0
-

Hello!

I have updated Nette from version 2.3 to 2.4 by instruction (https://doc.nette.org/…tions/to-2-4).

However, I have some problems with new version. I used the deprecated class in my code – nette/deprecated/src/Environment.php.

  1. The class deprecated but what do I use instead? The instruction and code nothing says about replace instance.
  2. How I can get the Container instance outside Presenters? I can replace some code pieces by Container data, but I did not find information about getting Container instance.

Thanks.

dkorpar
Member | 136
+
+2
-

using container directly is highly discouraged, you should pass only services you need through constructor and not use container at all in code. See: https://doc.nette.org/…introduction. If U have to much of those in presenters then you have also a different kind of problem that won't solve just getting rid of container…

Workaround can be to inject container to presenter and work with it, but that's a bad practice and you should avoid doing it…

From what I see you have a problem in architecture from start (not using DI at all) and this will probably be a pain to resolve.

Last edited by dkorpar (2022-08-02 15:54)

Adapter
Member | 3
+
0
-

using container directly is highly discouraged
___
I completely agree with this, but the project is huge and I can not refactor it now.

From what I see you have a problem in architecture from start (not using DI at all) and this will probably be a pain to resolve.
____

Exactly, the project has 15 years old legacy.

Marek Bartoš
Nette Blogger | 1275
+
0
-

Then use the deprecated class. And create plan for migration to DIC. You may as well replace Environment with your own static class and set Container to that class in your app/bootstrap.php so it does not stop you from further upgrades.

Adapter
Member | 3
+
0
-

You may as well replace Environment with your own static class and set Container to that class in your app/bootstrap.php so it does not stop you from further upgrades.
____
I have done it. However, I thought have more right way to getting the Container instance.
Thanks for the advices.