9

I am new to Laravel and trying to understand the difference between the boot() method and the register() method in App Service Provider class. I have searched all over but haven't been able to find a clear answer. I will really be grateful for a better explanation and kind cooperation.

1 Answers1

14

“After all providers have been registered, they are “booted”. This will fire the boot method on each provider. A common mistake when using service providers is attempting to use the services provided by another provider in the register method. Since, within the register method, we have no gurantee all other providers have been loaded, the service you are trying to use may not be available yet. So, service provider code that uses other services should always live in the boot method. The register method should only be used for, you guessed it, registering services with the container. Within the boot method, you may do whatever you like: register event listeners, include a routes file, register filters, or anything else you can imagine.”

So the register one is just for binding. The boot one is to actually trigger something to happen.

lucidlogic
  • 1,814
  • 13
  • 15
  • Thanks for explaning this issue! – dns_nx Jul 19 '19 at 12:38
  • 2
    You quoted someone else statement, but not giving the credit to him. https://laracasts.com/discuss/channels/general-discussion/difference-between-boot-and-register-method?page=1&replyId=4507 – Erick Sep 02 '22 at 12:11