{"id":513,"date":"2021-12-24T22:15:00","date_gmt":"2021-12-24T22:15:00","guid":{"rendered":"https:\/\/rishikantsri.in\/blog\/?p=513"},"modified":"2023-10-25T12:25:07","modified_gmt":"2023-10-25T12:25:07","slug":"laravel-8-dependency-injection-details-syntax-usages-real-time-samples-pros-and-cons","status":"publish","type":"post","link":"https:\/\/rishikantsri.in\/blog\/laravel-8-dependency-injection-details-syntax-usages-real-time-samples-pros-and-cons\/","title":{"rendered":"Laravel 8: Dependency Injection &#8211; Details, Syntax, Usages, Real-time Samples, Pros, and Cons"},"content":{"rendered":"\n<p>Dependency Injection is a crucial concept in Laravel, as it facilitates the practice of writing more modular, testable, and maintainable code. Laravel&#8217;s IoC (Inversion of Control) container plays a pivotal role in managing dependencies and injecting them where needed. Let&#8217;s delve into the details of Dependency Injection in Laravel.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Syntax<\/h3>\n\n\n\n<p>In Laravel, Dependency Injection is primarily achieved through constructor injection, method injection, and property injection.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Constructor Injection<\/h4>\n\n\n\n<p>Constructor injection is the most common and recommended way to inject dependencies into a class. To use constructor injection, simply type-hint the dependencies you need in the class&#8217;s constructor. Laravel&#8217;s IoC container will automatically resolve and inject these dependencies.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#b392f0;--cbp-line-number-width:9.90000057220459px;line-height:1.625rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1f1f1f\"><span style=\"background:#a37ced;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1f1f1f\">PHP<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"class UserController\n{\n    protected $userService;\n\n    public function __construct(UserService $userService)\n    {\n        $this-&gt;userService = $userService;\n    }\n}\" style=\"color:#b392f0;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki min-dark\" style=\"background-color: #1f1f1f\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F97583\">class<\/span><span style=\"color: #B392F0\"> UserController<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    <\/span><span style=\"color: #F97583\">protected<\/span><span style=\"color: #B392F0\"> $userService;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    <\/span><span style=\"color: #F97583\">public<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #F97583\">function<\/span><span style=\"color: #B392F0\"> __construct(<\/span><span style=\"color: #79B8FF\">UserService<\/span><span style=\"color: #B392F0\"> $userService)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        <\/span><span style=\"color: #79B8FF\">$this<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">userService <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #B392F0\"> $userService;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>In this example, the <code>UserController<\/code> class depends on the <code>UserService<\/code>, and Laravel&#8217;s IoC container will automatically resolve and inject an instance of <code>UserService<\/code> when a <code>UserController<\/code> is created.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Method Injection<\/h4>\n\n\n\n<p>In addition to constructor injection, you can use method injection to inject dependencies directly into controller methods. This is often used when you need a dependency for a specific method, rather than the entire class.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#b392f0;--cbp-line-number-width:19.800000190734863px;line-height:1.625rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1f1f1f\"><span style=\"background:#a37ced;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1f1f1f\">PHP<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"use App\\Services\\UserService;\n\nclass UserController\n{\n    public function show(UserService $userService, $id)\n    {\n        $user = $userService-&gt;find($id);\n        return view('users.show', compact('user'));\n    }\n}\" style=\"color:#b392f0;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki min-dark\" style=\"background-color: #1f1f1f\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F97583\">use<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #79B8FF\">App<\/span><span style=\"color: #BBBBBB\">\\<\/span><span style=\"color: #79B8FF\">Services<\/span><span style=\"color: #BBBBBB\">\\<\/span><span style=\"color: #79B8FF\">UserService<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F97583\">class<\/span><span style=\"color: #B392F0\"> UserController<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    <\/span><span style=\"color: #F97583\">public<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #F97583\">function<\/span><span style=\"color: #B392F0\"> show(<\/span><span style=\"color: #79B8FF\">UserService<\/span><span style=\"color: #B392F0\"> $userService<\/span><span style=\"color: #BBBBBB\">,<\/span><span style=\"color: #B392F0\"> $id)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        $user <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #B392F0\"> $userService<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">find<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #B392F0\">$id<\/span><span style=\"color: #BBBBBB\">)<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #B392F0\"> view<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #FFAB70\">&#39;users.show&#39;<\/span><span style=\"color: #BBBBBB\">,<\/span><span style=\"color: #B392F0\"> compact<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #FFAB70\">&#39;user&#39;<\/span><span style=\"color: #BBBBBB\">))<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>In this example, the <code>show<\/code> method is injected with the <code>UserService<\/code> instance when it is called.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Property Injection<\/h4>\n\n\n\n<p>Property injection is less common and less recommended than constructor and method injection. It involves injecting dependencies directly into class properties. While this approach is possible in Laravel, it&#8217;s generally discouraged because it makes the dependencies less explicit and harder to test.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#b392f0;--cbp-line-number-width:9.90000057220459px;line-height:1.625rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1f1f1f\"><span style=\"background:#a37ced;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1f1f1f\">PHP<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"class UserController\n{\n    public $userService;\n\n    public function __construct()\n    {\n        $this-&gt;userService = app(UserService::class);\n    }\n}\" style=\"color:#b392f0;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki min-dark\" style=\"background-color: #1f1f1f\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F97583\">class<\/span><span style=\"color: #B392F0\"> UserController<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    <\/span><span style=\"color: #F97583\">public<\/span><span style=\"color: #B392F0\"> $userService;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    <\/span><span style=\"color: #F97583\">public<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #F97583\">function<\/span><span style=\"color: #B392F0\"> __construct()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        <\/span><span style=\"color: #79B8FF\">$this<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">userService <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #B392F0\"> app<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #79B8FF\">UserService<\/span><span style=\"color: #F97583\">::class<\/span><span style=\"color: #BBBBBB\">)<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Usages<\/h3>\n\n\n\n<p>Dependency Injection is widely used in Laravel for various purposes, including:<\/p>\n\n\n\n<ol>\n<li><strong>Controller Actions<\/strong>: Injecting services, repositories, or data access objects into controller methods to handle specific business logic.<\/li>\n\n\n\n<li><strong>Middleware<\/strong>: Injecting middleware dependencies to modify the HTTP request and response at different stages of the application&#8217;s lifecycle.<\/li>\n\n\n\n<li><strong>Service Providers<\/strong>: Using service providers to bind interfaces to concrete implementations for automatic dependency resolution.<\/li>\n\n\n\n<li><strong>Unit Testing<\/strong>: Simplifying unit testing by injecting mock or stub dependencies for easy testing of classes.<\/li>\n\n\n\n<li><strong>View Composers<\/strong>: Injecting dependencies into Blade views to separate data retrieval and presentation logic.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Real-time Samples<\/h3>\n\n\n\n<p>Let&#8217;s explore some real-time examples of how Dependency Injection is used in a Laravel application:<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Controller Action<\/h4>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#b392f0;--cbp-line-number-width:19.800000190734863px;line-height:1.625rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1f1f1f\"><span style=\"background:#a37ced;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1f1f1f\">PHP<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"use App\\Services\\UserService;\n\nclass UserController\n{\n    protected $userService;\n\n    public function __construct(UserService $userService)\n    {\n        $this-&gt;userService = $userService;\n    }\n\n    public function show($id)\n    {\n        $user = $this-&gt;userService-&gt;find($id);\n        return view('users.show', compact('user'));\n    }\n}\" style=\"color:#b392f0;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki min-dark\" style=\"background-color: #1f1f1f\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F97583\">use<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #79B8FF\">App<\/span><span style=\"color: #BBBBBB\">\\<\/span><span style=\"color: #79B8FF\">Services<\/span><span style=\"color: #BBBBBB\">\\<\/span><span style=\"color: #79B8FF\">UserService<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F97583\">class<\/span><span style=\"color: #B392F0\"> UserController<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    <\/span><span style=\"color: #F97583\">protected<\/span><span style=\"color: #B392F0\"> $userService;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    <\/span><span style=\"color: #F97583\">public<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #F97583\">function<\/span><span style=\"color: #B392F0\"> __construct(<\/span><span style=\"color: #79B8FF\">UserService<\/span><span style=\"color: #B392F0\"> $userService)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        <\/span><span style=\"color: #79B8FF\">$this<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">userService <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #B392F0\"> $userService;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    }<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    <\/span><span style=\"color: #F97583\">public<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #F97583\">function<\/span><span style=\"color: #B392F0\"> show($id)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        $user <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #79B8FF\">$this<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">userService<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">find<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #B392F0\">$id<\/span><span style=\"color: #BBBBBB\">)<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #B392F0\"> view<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #FFAB70\">&#39;users.show&#39;<\/span><span style=\"color: #BBBBBB\">,<\/span><span style=\"color: #B392F0\"> compact<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #FFAB70\">&#39;user&#39;<\/span><span style=\"color: #BBBBBB\">))<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>In this example, the <code>UserService<\/code> is injected into the <code>UserController<\/code>, allowing the <code>show<\/code> method to retrieve and display user information.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Middleware<\/h4>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#b392f0;--cbp-line-number-width:19.800000190734863px;line-height:1.625rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1f1f1f\"><span style=\"background:#a37ced;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1f1f1f\">PHP<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"use Closure;\n\nclass LogRequestMiddleware\n{\n    public function handle($request, Closure $next, Logger $logger)\n    {\n        $logger-&gt;log($request-&gt;fullUrl());\n\n        return $next($request);\n    }\n}\" style=\"color:#b392f0;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki min-dark\" style=\"background-color: #1f1f1f\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F97583\">use<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #79B8FF\">Closure<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F97583\">class<\/span><span style=\"color: #B392F0\"> LogRequestMiddleware<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    <\/span><span style=\"color: #F97583\">public<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #F97583\">function<\/span><span style=\"color: #B392F0\"> handle($request<\/span><span style=\"color: #BBBBBB\">,<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #79B8FF\">Closure<\/span><span style=\"color: #B392F0\"> $next<\/span><span style=\"color: #BBBBBB\">,<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #79B8FF\">Logger<\/span><span style=\"color: #B392F0\"> $logger)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        $logger<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">log<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #B392F0\">$request<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">fullUrl<\/span><span style=\"color: #BBBBBB\">())<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #B392F0\"> $next($request);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>In this scenario, the <code>LogRequestMiddleware<\/code> uses Dependency Injection to inject a <code>Logger<\/code> instance to log incoming requests.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Service Provider<\/h4>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#b392f0;--cbp-line-number-width:19.800000190734863px;line-height:1.625rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1f1f1f\"><span style=\"background:#a37ced;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1f1f1f\">PHP<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"use Illuminate\\Support\\ServiceProvider;\nuse App\\Repositories\\UserRepository;\nuse App\\Repositories\\EloquentUserRepository;\n\nclass RepositoryServiceProvider extends ServiceProvider\n{\n    public function register()\n    {\n        $this-&gt;app-&gt;bind(UserRepository::class, EloquentUserRepository::class);\n    }\n}\" style=\"color:#b392f0;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki min-dark\" style=\"background-color: #1f1f1f\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F97583\">use<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #79B8FF\">Illuminate<\/span><span style=\"color: #BBBBBB\">\\<\/span><span style=\"color: #79B8FF\">Support<\/span><span style=\"color: #BBBBBB\">\\<\/span><span style=\"color: #79B8FF\">ServiceProvider<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F97583\">use<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #79B8FF\">App<\/span><span style=\"color: #BBBBBB\">\\<\/span><span style=\"color: #79B8FF\">Repositories<\/span><span style=\"color: #BBBBBB\">\\<\/span><span style=\"color: #79B8FF\">UserRepository<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F97583\">use<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #79B8FF\">App<\/span><span style=\"color: #BBBBBB\">\\<\/span><span style=\"color: #79B8FF\">Repositories<\/span><span style=\"color: #BBBBBB\">\\<\/span><span style=\"color: #79B8FF\">EloquentUserRepository<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F97583\">class<\/span><span style=\"color: #B392F0\"> RepositoryServiceProvider <\/span><span style=\"color: #F97583\">extends<\/span><span style=\"color: #B392F0\"> ServiceProvider<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    <\/span><span style=\"color: #F97583\">public<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #F97583\">function<\/span><span style=\"color: #B392F0\"> register()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        <\/span><span style=\"color: #79B8FF\">$this<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">app<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">bind<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #79B8FF\">UserRepository<\/span><span style=\"color: #F97583\">::class<\/span><span style=\"color: #BBBBBB\">,<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #79B8FF\">EloquentUserRepository<\/span><span style=\"color: #F97583\">::class<\/span><span style=\"color: #BBBBBB\">)<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>Here, a service provider binds an interface to a concrete implementation, allowing for automatic injection of the <code>UserRepository<\/code> where needed.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Pros<\/h3>\n\n\n\n<p>Dependency Injection in Laravel offers several advantages:<\/p>\n\n\n\n<ol>\n<li><strong>Modularity<\/strong>: Promotes modularity by breaking down large classes into smaller, more focused classes, making code easier to understand and maintain.<\/li>\n\n\n\n<li><strong>Testability<\/strong>: Facilitates unit testing by allowing for the injection of mock or stub dependencies, making it easier to write comprehensive tests.<\/li>\n\n\n\n<li><strong>Flexibility<\/strong>: Enables flexible and interchangeable implementations of dependencies, making it easier to adapt to changing requirements or switch out implementations.<\/li>\n\n\n\n<li><strong>Clearer Code<\/strong>: Dependencies are explicit, making it clear what a class requires to function correctly, improving code readability.<\/li>\n\n\n\n<li><strong>Easier Debugging<\/strong>: Reduces hidden dependencies and makes it easier to identify and fix issues.<\/li>\n\n\n\n<li><strong>Laravel&#8217;s IoC Container<\/strong>: Laravel&#8217;s built-in IoC container simplifies dependency management and resolution.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Cons<\/h3>\n\n\n\n<p>Despite the benefits, Dependency Injection can have some downsides:<\/p>\n\n\n\n<ol>\n<li><strong>Complexity<\/strong>: Overusing Dependency Injection can lead to an overly complex dependency graph, which may be hard to manage.<\/li>\n\n\n\n<li><strong>Learning Curve<\/strong>: It may require a learning curve for developers who are new to the concept of Dependency Injection.<\/li>\n\n\n\n<li><strong>Increased Boilerplate<\/strong>: Constructor injection can result in more boilerplate code, particularly in controller constructors.<\/li>\n\n\n\n<li><strong>Performance Impact<\/strong>: There can be a slight performance impact due to the resolution of dependencies through the IoC container.<\/li>\n\n\n\n<li><strong>Overhead<\/strong>: Using Dependency Injection for very simple classes can introduce unnecessary overhead.<\/li>\n\n\n\n<li><strong>Property Injection Pitfalls<\/strong>: Property injection can make dependencies less explicit and harder to test, so it&#8217;s generally discouraged.<\/li>\n<\/ol>\n\n\n\n<p>In conclusion, Dependency Injection is a fundamental concept in Laravel 8, promoting modularity, testability, and maintainability in your code. While it may introduce some complexity and a learning curve, the benefits of clear code, easy testing, and flexibility far outweigh the drawbacks. Laravel&#8217;s IoC container makes it easy to implement Dependency Injection in your applications, allowing you to build scalable, robust, and maintainable software. When used judiciously and thoughtfully, Dependency Injection can significantly improve your Laravel development experience and the quality of your code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Dependency Injection is a crucial concept in Laravel, as it facilitates the practice of writing more modular, testable, and maintainable code. Laravel&#8217;s IoC (Inversion of Control) container plays a pivotal role in managing dependencies and injecting them where needed. Let&#8217;s delve into the details of Dependency Injection in Laravel. Syntax In Laravel, Dependency Injection is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[61,3,4],"_links":{"self":[{"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/posts\/513"}],"collection":[{"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/comments?post=513"}],"version-history":[{"count":2,"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/posts\/513\/revisions"}],"predecessor-version":[{"id":516,"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/posts\/513\/revisions\/516"}],"wp:attachment":[{"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/media?parent=513"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/categories?post=513"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/tags?post=513"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}