You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've run into this problem when developing a package that replaces the default Laravel translator with a custom one (https://github.com/jrmajor/laravel-fluent). I can't figure out how to correctly register it in service container so that:
interface ComponentContract { }
class LaravelComponent implements ComponentContract { }
class UserlandComponent implements ComponentContract { }
Laravel components are registered in service container by simple string keys (app('component') returns instance of LaravelComponent). This happens in ComponentServiceProvider:
Both ComponentContract and LaravelComponent are then registered as aliases to component. This happens in Illuminate\Foundation\Application::registerCoreContainerAliases():
But this can’t be done in register(), because LaravelComponent is not registered yet. It works only when it’s done in boot().
To make it work in register() method, I would have to copy all the logic for building LaravelComponent from Laravel’s service provider and put it into the last line of the above code.
Alternatively, I can register it like it’s registered by Laravel:
But it makes it impossible to resolve LaravelComponent from the service container (it would always return UserlandComponent), which would break dependency injection for a class with type hinted LaravelComponent.
Edit: Container::extend() has the same problem.
Possible solution?
If Laravel registered it’s core services like this:
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Converted from issue #37812 to discussion
TL;DR
I've run into this problem when developing a package that replaces the default Laravel translator with a custom one (https://github.com/jrmajor/laravel-fluent). I can't figure out how to correctly register it in service container so that:
Introduction
Consider the following classes:
Laravel components are registered in service container by simple string keys (
app('component')
returns instance ofLaravelComponent
). This happens inComponentServiceProvider
:Both
ComponentContract
andLaravelComponent
are then registered as aliases tocomponent
. This happens inIlluminate\Foundation\Application::registerCoreContainerAliases()
:Edit: Actually, aliases are registered before service providers are loaded.
Description
I've run into this problem when developing a package that replaces the default Laravel translator with a custom one (https://github.com/jrmajor/laravel-fluent).
Using the example from the introduction, I want to replace
LaravelComponent
withUserlandComponent
in my application. It should work like this:I've managed to accomplish this by registering my component like that:
But this can’t be done in
register()
, becauseLaravelComponent
is not registered yet. It works only when it’s done inboot()
.To make it work in
register()
method, I would have to copy all the logic for buildingLaravelComponent
from Laravel’s service provider and put it into the last line of the above code.Alternatively, I can register it like it’s registered by Laravel:
But it makes it impossible to resolve
LaravelComponent
from the service container (it would always returnUserlandComponent
), which would break dependency injection for a class with type hintedLaravelComponent
.Edit:
Container::extend()
has the same problem.Possible solution?
If Laravel registered it’s core services like this:
These two lines in service provider would be enough to make everything work as expected:
The way Laravel does it currently seems strange to me. Is there any reason it's done like that?
Beta Was this translation helpful? Give feedback.
All reactions