Header Ads

Header ADS

Laravel Service Container and Service Provider

 Laravel-এর Service Container এবং Service Provider হলো এর Dependency Injection system-এর মূল ভিত্তি। নিচে বিস্তারিতভাবে বাংলায় ব্যাখ্যা করছি, উদাহরণসহ।


🧠 Service Container কী?

Laravel-এর Service Container হলো একটি dependency injection container। সহজভাবে বললে, এটা এমন একটা জায়গা যেখানে তুমি class বা interface রেজিস্টার করতে পারো, এবং Laravel প্রয়োজনে এই class গুলোকে automatically instantiate করে দেয়।

এর মূল সুবিধা:

  • Dependency injection সহজে করা যায়

  • Code loosely coupled হয়

  • Testing সহজ হয়


উদাহরণ: সাধারণভাবে Dependency Inject করা

ধরি তুমি একটি PaymentService class বানিয়েছো:

php
// app/Services/PaymentService.php namespace App\Services; class PaymentService { public function process($amount) { return "Processing ৳{$amount}"; } }

এখন তুমি একটি Controller-এ এটা ব্যবহার করতে চাও:

php
use App\Services\PaymentService; class OrderController extends Controller { public function checkout(PaymentService $paymentService) { return $paymentService->process(500); } }

Laravel নিজে থেকেই PaymentService instantiate করে দিবে Service Container-এর মাধ্যমে।


🧩 Service Provider কী?

Service Provider হলো Laravel-এর এমন একটা জায়গা, যেখানে তুমি নিজের service বা binding গুলো Service Container-এ রেজিস্টার করো।

সব Service Provider App\Providers ডিরেক্টরির ভিতরে থাকে এবং AppServiceProvider.php তে তুমি অনেক customization করতে পারো।


উদাহরণ: Custom Service এবং Provider

Step 1: একটা Custom Service তৈরি করো


// app/Services/CustomLogger.php namespace App\Services; class CustomLogger { public function log($msg) { \Log::info("Custom Log: " . $msg); } }

Step 2: একটি Custom Service Provider তৈরি করো


php artisan make:provider CustomLoggerServiceProvider

Step 3: Provider-এ Binding করো


// app/Providers/CustomLoggerServiceProvider.php namespace App\Providers; use Illuminate\Support\ServiceProvider; use App\Services\CustomLogger; class CustomLoggerServiceProvider extends ServiceProvider { public function register() { $this->app->bind('customlogger', function ($app) { return new CustomLogger(); }); } public function boot() { // } }

Step 4: Provider-টা config/app.php-এ রেজিস্টার করো


'providers' => [ App\Providers\CustomLoggerServiceProvider::class, ],

Step 5: যেকোনো জায়গা থেকে ব্যবহার করো


public function testLog() { $logger = app('customlogger'); $logger->log("Hello from custom logger!"); }

সংক্ষেপে পার্থক্য: 

বিষয়েService ContainerService Provider
কিDependency resolve করার containerBinding/registration করার জায়গা
ভূমিকাClass instantiate করেBinding container-এ রেজিস্টার করে
কোথায়Laravel core এ built-in
app/Providers ডিরেক্টরিতে থাকে


-------------------------------------------

এখন চল আমরা real-life উদাহরণ এবং কিছু advanced concept নিয়ে বিস্তারিত আলোচনা করি—যেমন:

  • ✅ Singleton Binding

  • ✅ Contextual Binding

  • ✅ Interface to Implementation Binding

  • ✅ Tagging

  • ✅ Facade ব্যবহার করা


✅ 1. Singleton Binding — একবারই Instantiate হবে

যখন তুমি চাও যে একটি class শুধুমাত্র একবার ইন্সট্যান্সিয়েট হবে এবং পুরো অ্যাপে সেই একই instance ব্যবহার হবে।


🔧 উদাহরণ:


$this->app->singleton('App\Services\PaymentService', function ($app) { return new \App\Services\PaymentService(); });

এখন যেখানেই তুমি PaymentService চাও, Laravel আগের তৈরি করা instance ই দেয়। এটি Performance ও Memory usage-এর জন্য ভালো।


✅ 2. Contextual Binding — নির্দিষ্ট Context অনুযায়ী ভিন্ন Dependency

ধরো তোমার কাছে দুটি class আছে যাদের একই interface implement করতে হয়, কিন্তু ভিন্ন ভিন্ন Controller এ ভিন্ন Implementation দরকার।


🔧 উদাহরণ:

Step 1: Interface তৈরি করো


// app/Contracts/PaymentGatewayInterface.php namespace App\Contracts; interface PaymentGatewayInterface { public function pay($amount); }

Step 2: দুইটা Implementation


// app/Services/StripePayment.php namespace App\Services; use App\Contracts\PaymentGatewayInterface; class StripePayment implements PaymentGatewayInterface { public function pay($amount) { return "Paid ৳{$amount} via Stripe"; } } // app/Services/PaypalPayment.php namespace App\Services; use App\Contracts\PaymentGatewayInterface; class PaypalPayment implements PaymentGatewayInterface { public function pay($amount) { return "Paid ৳{$amount} via PayPal"; } }

Step 3: দুইটা Controller


// StripeController.php public function __construct(PaymentGatewayInterface $gateway) { $this->gateway = $gateway; } // PaypalController.php public function __construct(PaymentGatewayInterface $gateway) { $this->gateway = $gateway; }

Step 4: Contextual Binding করো


$this->app->when(StripeController::class) ->needs(PaymentGatewayInterface::class) ->give(StripePayment::class); $this->app->when(PaypalController::class) ->needs(PaymentGatewayInterface::class) ->give(PaypalPayment::class);

এখন StripeController-এ StripePayment যাবে আর PaypalController-এ PaypalPayment!


✅ 3. Interface to Implementation Binding

Laravel আপনাকে Interface-based binding করতে দেয়। এটি SOLID principles (বিশেষত Dependency Inversion) অনুসরণ করে।


উদাহরণ:


$this->app->bind( App\Contracts\PaymentGatewayInterface::class, App\Services\StripePayment::class );

এখন Laravel জানে, যখনই PaymentGatewayInterface চাই, তখন StripePayment দিতে হবে।


✅ 4. Tagging — একাধিক Service Group করা

Tagging দিয়ে তুমি একাধিক সার্ভিসকে একটা tag এ গ্রুপ করে একসাথে access করতে পারো।


উদাহরণ:


$this->app->bind('service.one', ServiceOne::class); $this->app->bind('service.two', ServiceTwo::class); $this->app->tag(['service.one', 'service.two'], 'grouped.services');

পরে ব্যবহার:


$services = $this->app->tagged('grouped.services'); foreach ($services as $service) { $service->run(); }

✅ 5. Facade ব্যবহার করে Binding Access

Facade হল Laravel-এর সরল উপায়ে service container-এর মধ্যে থাকা ক্লাস গুলোকে access করা।

উদাহরণ:


// Binding $this->app->bind('customlogger', function () { return new \App\Services\CustomLogger; }); // Facade তৈরি class CustomLoggerFacade extends \Illuminate\Support\Facades\Facade { protected static function getFacadeAccessor() { return 'customlogger'; } }

এখন তুমি সহজে ব্যবহার করতে পারো:


CustomLoggerFacade::log('This is a log');

উপসংহার

Conceptব্যবহারের ক্ষেত্র
singletonএকবারই instance দরকার
contextual bindingএকই interface-এর জন্য ভিন্ন class
interface bindingloosely coupled architecture
taggingএকাধিক class group করে ব্যবহার
facadeelegant static style access

No comments

Theme images by fpm. Powered by Blogger.