Thursday, May 25, 2023
HomePHPDependence Shot Improvements (Symfony Blog Site)

Dependence Shot Improvements (Symfony Blog Site)


The Solution Container is the essential function that makes Symfony applications so
quickly and also adaptable. In Symfony 6.3 we have actually boosted it with a great deal of brand-new attributes.

The Autowire attibute was presented in Symfony 6.1 and also enables to autowire
solutions, criteria and also expressions. In Symfony 6.3, it can likewise autowire setting
variables (by means of the env alternative). Likewise, criteria are currently autowired making use of the
brand-new param alternative:


Alan Poulain

Added by
Alan Poulain

in # 49411

Solution pen names enable you to utilize solutions utilizing your very own custom-made solution ID.
rather than the initial ID provided to the solution. In Symfony 6.3 we’re including a.
brand-new #[AsAlias] feature so you can specify pen names straight in your code:

When making use of #[AsAlias] feature, you might leave out passing id debate if the.
solution course carries out specifically one user interface. In those situations, the FQCN of the.
user interface will certainly be made use of as the pen names.

When making use of some course as its very own solution manufacturing facility, you can utilize the brand-new.
fitter alternative of the #[Autoconfigure] to specify the name of the.
course technique that serves as its fitter:


Nicolas Grekas

Added by
Nicolas Grekas

in # 48710

In Symfony 6.3 we have actually boosted the #[Autowire] feature so you can nest.
various other autowiring-related features right into it. The copying programs this.
function at work making use of all the readily available choices:


Nicolas Grekas

In the Dependence Shot part, “solution closures” are closures that.
return a solution. They can be found in useful when taking care of idleness on the customer side.
In Symfony 6.3, we have actually included assistance for autowiring solutions as closures making use of.
the #[AutowireServiceClosure] feature:

This will certainly produce a closure that returns the solution my_service when called.

It’s likewise rather usual to have a solution approve a closure with a certain.
trademark as debate. In Symfony 6.3, you can produce such closures making use of.
the #[AutowireCallable] feature:

This will certainly produce a closure that will certainly have the very same trademark as the technique, so.
that calling it will certainly onward to the myMethod() on the solution my_service
This sort of closure-injection is not careless by default: my_service will certainly be.
instantiated when developing the $ callable debate. If you wish to make it.
careless, you can utilize the careless alternative:

This will certainly produce a closure that will certainly instantiate the my_service solution.
just when the closure is called.


Nicolas Grekas

Added by
Nicolas Grekas

in # 49632

Practical user interfaces are user interfaces with a solitary technique. They are conceptually.
extremely comparable to a closure other than that their only technique has a name, and also they.
can be made use of as type-hints.

The #[AutowireCallable] feature can be made use of to produce an adapter for a.
useful user interface. As an example, if you have the complying with useful.
user interface:

You can utilize the #[AutowireCallable] credit to produce an adapter for it:

Also if my_service does not execute UriExpanderInterface, the.
$ expander debate will certainly be a circumstances of UriExpanderInterface created.
by Symfony. Calling its broaden() technique will certainly onward to the myMethod()
technique of the my_service solution.

Assistance for creating such adapters in YAML, XML or PHP is likewise offered.


Nicolas Grekas

The #[Autowire] feature can be made use of to inform just how solutions need to be autowired.
In Symfony 6.3, we have actually included assistance for autowiring careless solutions making use of the careless
alternative:

This will certainly produce a careless solution that will certainly be instantiated just when the $ solution
debate is in fact made use of. This functions by creating a proxy course that carries out.
MyServiceInterface and also forwards all technique phones call to the real solution.
When targeting a disagreement with numerous feasible kinds, you can utilize the careless
alternative with a class-string worth to define which kind ought to be created:

This will certainly produce a proxy course that carries out just FirstServiceInterface

This function enables solutions that do not constantly eat their dependences to boot up.
them just when in fact required. This can give a considerable efficiency increase,.
specifically with solutions that are costly to boot up.


Jules Pietri

Added by
Jules Pietri

in # 47719

An additional brand-new function pertaining to the Solution Container is that you can currently.
deprecate criteria. This serves e.g. for expansions that wish to relabel.
or get rid of criteria, so they can advise customers regarding this future modification prior to doing it:

Think about utilizing this alternative if you wish to change your short-term criteria.
right into construct criteria, an additional brand-new function presented in Symfony 6.3.

When setting up solutions in the container, you can utilize the leave out alternative.
to inform Symfony to not develop solutions to several courses. This alternative comes.
in useful when omitting whole directory sites (e.g. src/Entity/). Nevertheless, if.
you simply wish to leave out some certain courses, in Symfony 6.3 you can likewise do.
that with the brand-new #[Exclude] feature:


Kevin Bond

Added by
Kevin Bond

in # 49433

In Symfony 6.3, the #[Autowire] feature can be included develop your very own.
custom-made autowiring assistants. As an example, consider this instance that produces a.
custom-made credit to autowire databases:

And after that, utilize it similar to this in your job:


Many thanks to all factors that boosted Dependence Shot in Symfony 6.3.
and also unique many thanks to Nicolas Grekas that likewise added fifty percent of the components.
of this post to clarify one of the most sophisticated attributes.

RELATED ARTICLES

Most Popular

Recent Comments