Understanding Laravel View Composers: Where does my view composer belong? Understanding Laravel View Composers: Where does my view composer belong? laravel laravel

Understanding Laravel View Composers: Where does my view composer belong?


Citing the answer given by a user in this question:

Difference between boot and register method?

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


They should be placed in the boot method. The register method does not guarantee dependencies will be resolved when you may need them. With the boot method you have that guarantee. And as you mentioned, ideally you should create a separate service provider for the view composers.