Using contracts resolved in AppServiceProvider

I am trying to use a contract which is resolved in the AppServiceProvider, in the Filament\AdminPanelProvider panel config. But it is not resolved at this point as Filament\AdminPanelProvider seems to be registered first, even though it appears after the main provider in the config/app.php How can I change this order?
Solution:
It is in the boot() method.
Jump to solution
14 Replies
Patrick Boivin
Can you share an example of what you're trying to do?
trovster
trovsterOP2y ago
$this->app->make(MyContract::class) inside the panel() method fails because MyContract::class can't be resolved… Not sure what else I can say.
Patrick Boivin
What is the use of MyContract::class... what are you using it for?
trovster
trovsterOP2y ago
$this->app->singleton(ColourContract::class, static fn (Container $app) => new ColourService(
$app->get('config')->get('site.general.colors', [
'blue' => '#2490BA',
])
));
$this->app->singleton(ColourContract::class, static fn (Container $app) => new ColourService(
$app->get('config')->get('site.general.colors', [
'blue' => '#2490BA',
])
));
->colors([
'primary' => value(function () {
return $this->app->make(Colour::class)->current();
}),
])
->colors([
'primary' => value(function () {
return $this->app->make(Colour::class)->current();
}),
])
ralphjsmit
ralphjsmit2y ago
Shouldn't it be $this->app->make(ColourContract::class) instead of Colour::class?
trovster
trovsterOP2y ago
Yes, that's what I have tried. ColourContract is an alias. Sorry, I just undid my deletion of the code.
Patrick Boivin
@trovster Not sure... but have you tried bootUsing?
return $panel
// ...
->bootUsing(function ($panel) {
$panel->colors( ... );
});
return $panel
// ...
->bootUsing(function ($panel) {
$panel->colors( ... );
});
I think you'll have all bindings available in the boot phase
ralphjsmit
ralphjsmit2y ago
Is your $this->app->singleton() placed in the boot() method or in the register() method?
Solution
trovster
trovster2y ago
It is in the boot() method.
ralphjsmit
ralphjsmit2y ago
OK, so then you should move it to the register method Laravel makes the difference specifically, so that you can register all singletons and contracts in the register method, and then in the boot() method you can trust Laravel that all these singletons and contracts have been registered
trovster
trovsterOP2y ago
Perfect, thanks 🙂
Daniel
Daniel15mo ago
i've just tried your suggestion but the colors doens't change. any ideas?
Patrick Boivin
Patrick Boivin15mo ago
If I remember correctly, bootUsing() might be too late in the process... I'll see if I can find you another reference This one is using FilamentColor from AppServiceProvider : https://discord.com/channels/883083792112300104/1150491658044051606/1150531056680829049
Daniel
Daniel15mo ago
perfect, thanks a lot!

Did you find this page helpful?