Laravel
Beginner
1 min read
Service Container and Service Providers
Example
<?php
// app/Providers/AppServiceProvider.php
namespace App\Providers;
use App\Contracts\PaymentGateway;
use App\Services\StripePaymentGateway;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
public function register(): void
{
// Bind the interface to a concrete implementation
$this->app->bind(PaymentGateway::class, function ($app) {
return new StripePaymentGateway(
config('services.stripe.secret')
);
});
// Register a singleton (resolved only once)
$this->app->singleton('analytics', function ($app) {
return new \App\Services\AnalyticsService(
config('services.analytics.key')
);
});
}
public function boot(): void
{
// Actions run after all providers are registered
\Illuminate\Pagination\Paginator::useBootstrapFive();
}
}
Related Resources
Laravel Reference
Complete tag & property list
Laravel How-To Guides
Step-by-step practical guides
Laravel Exercises
Practice what you've learned
More in Laravel