aliqsyed's avatar

Difference between boot and register method?

I don't quite understand the difference between the boot() method and the register() method in Service Provider classes. I have searched all over but haven't really been able to find a clear answer.

0 likes
7 replies
noeldiaz's avatar
Level 23

I actually just learned the difference last night from Taylor's book. Here is an excerpt about it:

“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.

54 likes
mozew's avatar

Is this book write all languages? Arabic, Persian, English......

1 like
Ambit's avatar

Can I say that if I am trying to register service that is not dependent of other services - I should put it's registration into "register" method, but if my service depends of other services I should put its registration it into "boot" method ? And of course all other bootstrap logic should go to "boot" method ?

ashish26's avatar

Nice Explaination! Would like to refer other developer!

Please or to participate in this conversation.