New

Just launched: Vueblocks, a brand-new search engine for Vue packages! Explore Vueblocks →

enzyme/laravel-binder

A service container contextual binding helper for Laravel 5.
25 1
Install
composer require enzyme/laravel-binder
Latest Version:v1.0.0
PHP:>=5.5.0
Laravel:5
License:MIT
Last Updated:Sep 22, 2016
Links: GitHub  ·  Packagist
Maintainer: r3oath

Binder

Build Status Coverage Status Scrutinizer Code Quality

A contextual binding helper for Laravel 5.

Example

From the service provider.
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
// 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!

Related Packages

rtroncoso/laravel-context

Provides Service Provider binding depending on a context

620 51
laradic/service-provider

Laravel 5 service provider on steroids. Lots of extras.

286 0
valorin/random

Random is a simple helper package designed to make it easy to generate a range o...

436,338 234
php
cline/variable-keys

Laravel Blueprint macros for variable primary key and morph types

24,232 0
php
cline/struct

Lean, attribute-driven Laravel data objects with explicit validation and Eloquen...

19,286 0
php

Version History

Version Released PHP Laravel License
v1.0.0 >=5.5.0 ^5.2 MIT
v0.2.1 >=5.5.0 ^5.2 MIT
v0.2.0 >=5.5.0 ^5.2 MIT
0.1.0 >=5.5.0 ^5.2 MIT