Understanding Laravel Dependency Injection | Dependency Injection In Laravel In Hindi

Other Blogs

Blogs ❯❯ Laravel

Image could not load

Laravel Dependency Injection

अगर आप Laravel use करते हैं तो आपने documentation में एक word जरूर सुना होगा : Dependency Injection . इस word का मतलब क्या है और क्या impact है इसका सब कुछ जानेंगे इस article में।

Dependency Injection In Laravel

Dependency Injection (DI) एक design pattern है जो Inversion of Control (IoC) को implement करता है। ये pattern dependencies dependencies को खुद से create करने के वजाय class को अपनी dependencies बाहर से लेने के लिए allow करता है।

Laravel में Dependency Injection का use class dependencies और loose coupling achieve करने के लिए extensively use होता है।

Need of Dependency Injection

  1. Loose Coupling : DI classes को specific implementations पर कम depend और abstractions (interfaces) पर ज्यादा depend होने देता है। इसे code ज्यादा flexible और maintainable होता है।

  2. Testability : DI से आपके classes के लिए unit tests लिखना आसान हो जाता है क्योंकि आप mock dependencies inject कर सकते हैं।

  3. Single Responsibility Principle : DI हर class को अपनी primary responsibility पर focus करने देता है और secondary responsibilities के लिए दूसरी classes पर rely करने देता है।

  4. Code Reusability : Dependencies को आपके application के अलग - अलग parts में reuse किया जा सकता है बिना उन्हें दुबारा create किये।

Laravel Dependency Injection Example

Suppose हमें अपने e-commerce website के लिए payment process करना है। इसके लिए सबसे पहले हम PaymentService class बना रहे हैं जिसे app/Services directory में रखेंगे।-

mkdir app/Services

namespace App\Services; class PaymentService { public function process_payment($amount) { // Payment processing logic here return "Processing payment of $amount."; } }

Next, आपको service provider बनाना होगा , Service provider में आप अपनी service को container के साथ register करते हैं। ताकि Laravel automatically dependency inject और resolve कर सके।

php artisan make:provider PaymentServiceProvider

इस command से PaymentServiceProvider class generate होगी जो app/Providers directory में होगी , जिसमे नीचे दिया गए code रखेंगे -

namespace App\Providers; use Illuminate\Support\ServiceProvider; use App\Services\PaymentService; class PaymentServiceProvider extends ServiceProvider { /** * Register any application services. * * @return void */ public function register() { $this->app->singleton(PaymentService::class, function ($app) { return new PaymentService(); }); } /** * Bootstrap any application services. * * @return void */ public function boot() { // } }

Service provider को register करने के लिए config/app.php file में providers array में PaymentServiceProvider class को add करें।

'providers' => [ // Other Service Providers App\Providers\PaymentServiceProvider::class, ],

अब आप अपनी PaymentService को application के किसी भी part में use कर सकते हैं , Example के लिए हम इसे एक controller में कुछ इस तरह से access कर सकते हैं।

namespace App\Http\Controllers; use App\Services\PaymentService; class PaymentController extends Controller { protected $paymentService; public function __construct(PaymentService $paymentService) { $this->paymentService = $paymentService; } public function process($amount) { $result = $this->paymentService->process_payment($amount); return response()->json(['message' => $result]); } }

__construct() में , laravel आपके provider के लिए automatically class को resolve करके आपको PaymentService class का object return करेगा।

Advantages of Dependency Injection

  1. Flexible Code : आप easily dependencies को change कर सकते हैं बिना main code को touch किये।

  2. Better Testing : आप testing करते समय easily mock dependencies को inject कर सकते हैं।

  3. Cleaner Code : Dependencies का creation और management centralize हो जाता है जो code को cleaner और maintainable बनाता है।

Recent Blogs

Loading ...

Hey ! I'm Rahul founder of learnhindituts.com. Working in IT industry more than 4.5 years. I love to talk about programming as well as writing technical tutorials and blogs that can help to others .... keep learning :)

Get connected with me - LinkedIn Twitter Instagram Facebook

Your Thought ?

Please wait . . .

    0 Comment(s) found !