{"flag":true,"single":true,"pageTitle":"What is service container, Service Provider, AppServiceProvider in laravel","post":{"id":104,"user_id":"1","slug":"what-is-service-container-in-laravel-lpvk","title":"What is service container, Service Provider, AppServiceProvider in laravel","body":"<p><strong>service provider:<\/strong><br>&gt; A service that is provided to laravel ie Cache Service, Mail Service . They tell Laravel how to boot (initialize) services, bind things into the service container, register events, macros, etc.<br><strong>Service container<\/strong> (its manage class dependencies)<br>&nbsp; &nbsp; A container that make objects of service provider so we can use it in whole application<\/p>\r\n<p>######################<\/p>\r\n<p><strong>AppServiceProvider: <\/strong>app\/Providers\/AppServiceProvider.php<br>It&rsquo;s automatically loaded because it&rsquo;s registered in <strong>config\/app.php <\/strong>under providers.<strong><br><\/strong><\/p>\r\n<p>It has two methods, <strong>register() <\/strong>For binding services, boot() &rarr; For initialization after services are registered<\/p>\r\n<ol>\r\n<li>FORCE HTTPS<br><br>\r\n<pre class=\"language-markup\"><code>if ($this-&gt;app-&gt;environment('production')) {\r\n        URL::forceScheme('https');\r\n}<\/code><\/pre>\r\n&nbsp;<\/li>\r\n<li>Custom Blade Directives<br>\r\n<pre class=\"language-markup\"><code>use Illuminate\\Support\\Facades\\Blade;\r\npublic function boot(): void\r\n{\r\n    Blade::directive('currency', function ($amount) {\r\n        return \"&lt;?php echo '$' . number_format($amount, 2); ?&gt;\";\r\n    });\r\n}\r\n\r\n@currency(1500)  &lt;!-- outputs $1,500.00 --&gt;\r\n<\/code><\/pre>\r\n<\/li>\r\n<li>Cutom validation rule<br>\r\n<pre class=\"language-markup\"><code>use Illuminate\\Support\\Facades\\Validator;\r\npublic function boot(): void\r\n{\r\n    Validator::extend('even', function ($attribute, $value, $parameters, $validator) {\r\n        return $value % 2 === 0;\r\n    });\r\n}\r\n####\r\n$request-&gt;validate([\r\n    'number' =&gt; 'required|even'\r\n]);\r\n<\/code><\/pre>\r\n<\/li>\r\n<li>Share data with views<br>\r\n<pre class=\"language-markup\"><code>\/\/ Share the `today_count` variable globally across all views\r\n\/\/ use any viewname in place of * to share only with that ie index\r\n\\Illuminate\\Support\\Facades\\View::composer('*', function ($view) { \r\n    $todo_list = \\App\\Models\\Todo::where(\"is_completed\",\"0\");\r\n\r\n    if (\\Illuminate\\Support\\Facades\\Auth::check()) {\r\n            $todo_list = $todo_list-&gt;where(\"user_id\", $user_id);\r\n    }\r\n    $today_count = $todo_list-&gt;count();\r\n    $view-&gt;with('today_count', $today_count);\r\n});<\/code><\/pre>\r\n<\/li>\r\n<\/ol>","category_id":"2","is_private":"0","created_at":"2023-04-05T00:20:57.000000Z","updated_at":"2025-09-08T20:55:39.000000Z","category":{"id":2,"user_id":"1","name":"Laravel Core","slug":"laravel-nhyt","parent_id":"1","created_at":"2023-03-14T03:58:27.000000Z","updated_at":"2023-03-20T11:30:50.000000Z"},"user":{"id":1,"name":"R GONDAL","email":"rizikmw@gmail.com","email_verified_at":null,"two_factor_confirmed_at":null,"current_team_id":"1","profile_photo_path":null,"created_at":"2023-03-12T10:49:33.000000Z","updated_at":"2025-01-10T12:59:00.000000Z","profile_photo_url":"https:\/\/ui-avatars.com\/api\/?name=R+G&color=7F9CF5&background=EBF4FF"}},"pageDesc":"service provider:&gt; A service that is provided to laravel ie Cache Service, Mail Service . They tell Laravel how to boot (initialize) serv - What is service container, Service Provider, AppServiceProvider in laravel (Updated: September 8, 2025) - Read more about What is service container, Service Provider, AppServiceProvider in laravel at my programming site [SITE]","categories":[]}