ServiceProvider class abstract
The Service Provider Contract.
Service providers are the central place to configure your application. All of the Magic framework's core services are bootstrapped via providers, and you should do the same for your own services.
Think of providers as the "blueprint" for your modules. They tell the framework: "Here's what services I offer, and here's how to boot them up."
Creating a Provider
class AuthServiceProvider extends ServiceProvider {
AuthServiceProvider(super.app);
@override
void register() {
// Bind your services here
app.singleton('auth', () => AuthManager());
app.bind('guard', () => SessionGuard());
}
@override
void boot() {
// Run code after ALL providers are registered
final auth = app.make<AuthManager>('auth');
auth.configure();
}
}
Registering Providers
Providers are registered in your application bootstrap:
void main() {
final app = MagicApp.instance;
app.register(AuthServiceProvider(app));
app.register(RouteServiceProvider(app));
app.boot();
runApp(MyApp());
}
The Lifecycle
-
Register Phase:
register()is called immediately when you callapp.register(provider). Bind your services here. -
Boot Phase:
boot()is called when you callapp.boot(), AFTER all providers have been registered. This is where you can safely resolve services from other providers.
Constructors
- ServiceProvider(MagicApp app)
- Create a new service provider instance.
Properties
Methods
-
boot(
) → Future< void> - Bootstrap any application services.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
register(
) → void - Register any application services.
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited