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

  1. Register Phase: register() is called immediately when you call app.register(provider). Bind your services here.

  2. Boot Phase: boot() is called when you call app.boot(), AFTER all providers have been registered. This is where you can safely resolve services from other providers.

Implementers

Constructors

ServiceProvider(MagicApp app)
Create a new service provider instance.

Properties

app MagicApp
Reference to the application container.
final
hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

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