Where to Keep Your Application Bindings


You want to keep your application bindings in a convenient place.

You're binding multiple things to the IoC container and aren't sure the best place to consolidate all these bindings.


Create a helper style file.

Follow the same instructions as Creating a Helpers File and create a app/bindings.php file.

The contents could be something like.

// Interface bindings
App::bind('FooInterface', 'FooClass');
App::bind('BarInterface', 'BarClass');

// A closure
App::bind('FooBar', function()
    return new MyApp\Junk\SimpleFooBar();

// Singletons
App::singleton('MedicalDictionary', function()
    return new Dictionary('english-medical-terms');


Don't forget service providers.

Service providers are the most common place to bind elements to the IoC container. This makes sense, especially for package development.

If you're creating an app/bindings.php helper file, it's a good idea to comment within the file all of your service provider bindings. Then you still have one place to check for all the bindings.

Remember helper files are not loaded until the application is booted. See Understanding the Request Lifecycle. If you have bindings which are required by your own service providers, then you must create a service provider to bind the elements you'll need.

comments powered by Disqus