Laravel Service Container
Service Container (সার্ভিস কন্টেইনার) Laravel-এর সবচেয়ে শক্তিশালী ফিচারগুলোর মধ্যে একটি, যা ডিপেন্ডেন্সি ইনজেকশন এবং ক্লাস রেজোলিউশন ম্যানেজ করে। এটি মূলত Laravel-এর ডিপেন্ডেন্সি ম্যানেজমেন্ট সিস্টেম।
সার্ভিস কন্টেইনার কি?
এটি একটি পাওয়ারফুল ডিপেন্ডেন্সি ইনজেকশন টুল যা:
ক্লাস ডিপেন্ডেন্সিজ ম্যানেজ করে
ক্লাস ইনস্ট্যান্স তৈরি করে
ডিপেন্ডেন্সিজ অটোমেটিকভাবে রেজলভ করে
কেন এটি গুরুত্বপূর্ণ?
লুজ কাপলিং: কম্পোনেন্টস আলাদা থাকে
টেস্টেবিলিটি: মকিং/স্টাবিং সহজ
মেইনটেনেবিলিটি: কোড সংগঠিত রাখে
বেসিক ব্যবহার
১. বাইন্ডিং রেজিস্ট্রেশন
// Service Provider-এ $this->app->bind('HelpSpot\API', function ($app) { return new HelpSpot\API($app->make('HttpClient')); });
২. রেজোল্ভ করা
$api = app()->make('HelpSpot\API'); // অথবা $api = resolve('HelpSpot\API');
বাইন্ডিং টাইপস
১. Simple Binding
$this->app->bind(Service::class, function ($app) { return new ServiceImplementation(); });
২. Singleton Binding
$this->app->singleton(Service::class, function ($app) { return new SingletonService(); });
৩. Instance Binding
$service = new ServiceImplementation(); $this->app->instance(Service::class, $service);
৪. Interface Binding
$this->app->bind( PaymentGatewayInterface::class, StripePaymentGateway::class );
ডিপেন্ডেন্সি ইনজেকশন উদাহরণ
class UserController extends Controller { protected $payment; public function __construct(PaymentGateway $payment) { $this->payment = $payment; } public function store() { $this->payment->charge(100); } }
কন্টেইনার রেজোলিউশন প্রক্রিয়া
Reflection ব্যবহার করে ক্লাস analyze করে
কনস্ট্রাক্টর ডিপেন্ডেন্সিজ চেক করে
রিকার্সিভলি ডিপেন্ডেন্সিজ রেজলভ করে
অবজেক্ট ইনস্ট্যান্টিয়েট করে রিটার্ন করে
অ্যাডভান্সড ফিচার
১. কনটেক্সচুয়াল বাইন্ডিং
$this->app->when(PhotoController::class) ->needs(Filesystem::class) ->give(function () { return Storage::disk('local'); });
২. ট্যাগিং
$this->app->tag([SpeedReport::class, MemoryReport::class], 'reports'); $this->app->bind('ReportAggregator', function ($app) { return new ReportAggregator($app->tagged('reports')); });
৩. এক্সটেন্ডিং বাইন্ডিং
$this->app->extend(Service::class, function ($service, $app) { return new DecoratedService($service); });
প্র্যাকটিকাল ব্যবহার
কাস্টম ক্লাস রেজিস্টার
// AppServiceProvider.php public function register() { $this->app->bind(ImageProcessor::class, function ($app) { return new ImageProcessor( config('image.quality'), $app->make(FileSystem::class) ); }); }
ব্যবহারের উদাহরণ
// Controller-এ public function upload(ImageProcessor $processor) { $processor->compress(request()->file('image')); }
সার্ভিস কন্টেইনার vs ডিপেন্ডেন্সি ইনজেকশন
| Feature | Service Container | Dependency Injection |
|---|---|---|
| স্কোপ | অ্যাপ্লিকেশন জুড়ে | স্পেসিফিক ক্লাস/মেথড |
| কনফিগারেশন | প্রোভাইডারে রেজিস্টার | কনস্ট্রাক্টর/মেথড প্যারামিটার |
| লাইফসাইকেল | Singleton/Transient ম্যানেজ করে | প্রতিবার নতুন ইনস্ট্যান্স |
বেস্ট প্র্যাকটিস
ইন্টারফেস বাইন্ডিং ব্যবহার করুন
সিংগল্টন ব্যবহার করুন যেখানে প্রয়োজন
প্রোভাইডার ব্যবহার করে বাইন্ডিং রেজিস্টার করুন
কন্ট্রোলার-এ সরাসরি রেজল্ভ এড়িয়ে চলুন
No comments