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
Patrick Boivin16mo ago
Can you share an example of what you're trying to do?
trovster
trovsterOP16mo 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
Patrick Boivin16mo ago
What is the use of MyContract::class... what are you using it for?
trovster
trovsterOP16mo 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
ralphjsmit16mo ago
Shouldn't it be $this->app->make(ColourContract::class) instead of Colour::class?
trovster
trovsterOP16mo ago
Yes, that's what I have tried. ColourContract is an alias. Sorry, I just undid my deletion of the code.
Patrick Boivin
Patrick Boivin16mo ago
@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
ralphjsmit16mo ago
Is your $this->app->singleton() placed in the boot() method or in the register() method?
Solution
trovster
trovster16mo ago
It is in the boot() method.
ralphjsmit
ralphjsmit16mo 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
trovsterOP16mo ago
Perfect, thanks 🙂
Daniel
Daniel13mo ago
i've just tried your suggestion but the colors doens't change. any ideas?
Patrick Boivin
Patrick Boivin13mo 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
Daniel13mo ago
perfect, thanks a lot!
Want results from more Discord servers?
Add your server