Search code examples
laraveldeferredservice-provider

When to use defer service provider?


I read the laravel doc saying,

If your provider is only registering bindings in the service container, you may choose to defer its registration until one of the registered bindings is actually needed.

I don't understand what is "only registering bindings". I google and read many article, but I didn't find the answer. Could you help me to understand it!

Thank you very much^^


Solution

  • Here is the Laravel Documentation about Bindings. In short - it tells the service container what to return when some class or interface is needed. (You can understand more by reading about Dependency Injection and Dependency Injection in PHP). But Laravel Service providers are not only limited to binding classes in the container. Inside the providers you can point to routes folders, register view() composers, etc...

    If you don't have any other code in your Service provider besides registering bindings, than you can defer it so that the code would work faster. But if, for instance, you have a second routes folder that you register in service provider - then you should not defer registering the service provider.