"Laravel'in sihirli gücü nereden geliyor?" diye hiç merak ettiniz mi? İşte bu gücün kaynağı: Dependency Injection (DI) ve Service Container sistemi! Gelin bu güçlü mekanizmayı birlikte keşfedelim.
1. Dependency Injection Nedir? 🤔
Dependency Injection (Bağımlılık Enjeksiyonu), bir sınıfın ihtiyaç duyduğu bağımlılıkların dışarıdan verilmesi prensibidir. Laravel'de 3 temel DI yöntemi vardır:
Constructor Injection (En İyi Yöntem)
class PaymentController {
protected $paymentService;
public function __construct(PaymentService $paymentService) {
$this->paymentService = $paymentService;
}
}
Method Injection
public function process(PaymentService $paymentService) {
$paymentService->charge();
}
Setter Injection
public function setPaymentService(PaymentService $paymentService) {
$this->paymentService = $paymentService;
}
2. Service Container: Laravel'in Sihirli Kutusu 🎩✨
Laravel'in Service Container'ı, bağımlılıkları yöneten ve otomatik olarak çözümleyen güçlü bir sistemdir.
Basit Binding (Kayıt) Örneğiy
// AppServiceProvider içinde
$this->app->bind('PaymentService', function($app) {
return new PaymentService(config('payment.api_key'));
});
Singleton Binding
$this->app->singleton('Analytics', function() {
return new AnalyticsService();
});
Interface Binding
$this->app->bind(
PaymentGatewayInterface::class,
StripePaymentGateway::class
);
3. Container'ı Kullanma Yöntemleri 🔧
app() Helper ile
$payment = app('PaymentService');
make() Methodu ile
$payment = $this->app->make('PaymentService');
resolve() Helper ile
$payment = resolve('PaymentService');
4. Contextual Binding (Bağlamsal Bağlama) 🧩
Belirli durumlarda farklı implementasyonlar sağlamak için:
$this->app->when(EmailController::class)
->needs(MailerInterface::class)
->give(SesMailer::class);
5. Otomatik Çözümleme (Auto-Resolution) 🔄
Laravel, type-hint edilen sınıfları otomatik olarak çözümler:
// Laravel otomatik olarak UserRepository örneği oluşturur
public function __construct(UserRepository $users) {
$this->users = $users;
}
6. Pratik Kullanım Örnekleri 🛠️
Repository Pattern Uygulaması
$this->app->bind(
UserRepositoryInterface::class,
EloquentUserRepository::class
);
// Controller'da
public function __construct(UserRepositoryInterface $users) {
$this->users = $users;
}
Özel Config ile Servis Oluşturma
$this->app->singleton('Geocoder', function() {
return new GoogleMapsGeocoder(config('services.google.maps_key'));
});
7. Container Events (Olayları) 🎭
Container'ın çalışmasını izlemek için:
$this->app->resolving(function($object, $app) {
// Nesne çözümlendiğinde çalışır
});
$this->app->afterResolving(function($object, $app) {
// Çözümleme tamamlandığında çalışır
});
Güçlü ve Esnek Uygulamalar 🏗️
Laravel'in DI ve Service Container sistemi sayesinde:
- Daha test edilebilir kod 🧪
- Daha esnek yapı 🧘
- Daha temiz mimari 🧹
- Daha kolay bakım 🔧
Bu patternleri projelerinizde nasıl kullanıyorsunuz? Yorumlarda deneyimlerinizi paylaşın! 💬
Bir sonraki yazımız: 🚀 [Laravel'de API Resource: Veri Dönüşümü ve Formatlama] - API yanıtlarınızı profesyonelce yönetin!
#Laravel #DependencyInjection #ServiceContainer #DesignPatterns #PHP 🚀
+(42).png&w=320&q=50)


