https://github.com/enzyme/laravel-binder
A service container contextual binding helper for Laravel 5.
https://github.com/enzyme/laravel-binder
Last synced: 8 months ago
JSON representation
A service container contextual binding helper for Laravel 5.
- Host: GitHub
- URL: https://github.com/enzyme/laravel-binder
- Owner: enzyme
- License: mit
- Created: 2016-01-19T07:01:15.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2016-09-22T06:08:54.000Z (over 9 years ago)
- Last Synced: 2025-05-11T09:47:15.952Z (about 1 year ago)
- Language: PHP
- Size: 22.5 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Binder
[](https://travis-ci.org/enzyme/laravel-binder)
[](https://coveralls.io/github/enzyme/laravel-binder?branch=master)
[](https://scrutinizer-ci.com/g/enzyme/laravel-binder/?branch=master)
A contextual binding helper for Laravel 5.
# Example
#### From the service provider.
```php
use Enzyme\LaravelBinder\Binder;
// Inside the app service provider...
public function register()
{
$binder = new Binder($this->app);
$binder->setAlias(
'controller.listing',
'App\Http\Controllers\ListingController'
);
$binder->setAlias(
'repos.interface',
'Acme\Repositories\RepositoryInterface'
);
// Option 1 for binding, using aliases.
$binder->setBinding(
'repos.listing',
'repos.interface',
'Acme\Repositories\ListingRepository'
);
// Option 2 for binding, using FQNs.
$binder->setBinding(
'factories.listing',
'Acme\Factories\FactoryInterface',
'Acme\Factories\ListingFactory'
);
// Tell the service container that the ListingController
// needs the ListingRepository & ListingFactory.
$binder->setNeeds(
'controller.listing',
['repos.listing', 'factories.listing']
);
$binder->register();
}
```
#### From the controller
```php
// You don't need to inject the specific concrete classes, just the
// interfaces. The Binder + Service Container has taken care of it for you.
public function __construct(RepositoryInterface $repo, FactoryInterface $factory)
{
$this->repo = $repo;
$this->factory = $factory;
}
```
# What's the purpose?
With your service provider(s) now being the sole authority for which classes get what implementations, if you ever need to switch those implementations, it's dead simple and in one spot!