Ad

Laravel 5.1 Defer Service Provider Until Another Is Loaded

- 1 answer

I'm using the FormBuilder in Laravel 5.1. The HtmlServiceProvider states the Html and Form facades should defer loading until they are actually needed, which is fine.

However, I have created a FormServiceProvider which registers several macros that I will use in my views. This provider doesn't run any bindings, it simply records a few macros within the FormBuilder class.

However, by storing these macros using the boot method on my own service provider, it's loading the Form binding instantly (making the deferred loading pointless).

Question

Is there any way to defer the boot method in one service provider until another service provider has binded facades?

OR, is there a way to listen for specific binding (or aliasing) events, then manually run a service provider?

Ad

Answer

you can listen when container resolves object of any type or when container resolves objects of type XXX as below,

$this->app->resolving(function ($object, $app) {
    // Called when container resolves object of any type...
});

$this->app->resolving(FooBar::class, function (FooBar $fooBar, $app) {
    // Called when container resolves objects of type "FooBar"...
});

here is the DOC

Ad
source: stackoverflow.com
Ad