jeudi 29 octobre 2015

Laravel ServiceProvider bound arguments not passed through

I have the following ServiceProvider:

use App\Api\Session;

class ApiServiceProvider extends ApiServiceProvider
{
    public function register()
    {
        $accessKey = 'accessKey';
        $secretKey = 'secretKey';
        $userAgent = 'userAgent';
        $domain = 'domain';

        $this->app->bind('App\Api\Session', function (Application $app) use ($accessKey, $secretKey, $userAgent, $domain) {
            return new Session($accessKey, $secretKey, $userAgent, $domain);
        });
    }
}

As I'd like to be able to inject the App\Api\Session class into my controllers/commands, rather than having to create new instances and pass in the $accessKey, $secretKey, $userAgent and $domain each time I want to use them. For example:

use App\Api\Session;

class SessionController
{
    public function __construct(Session $session)
    {
        $this->session = $session;
    }

    public function store()
    {
        $response = $this->session->create($credentials);

        // etc...
    }
}

I thought this approach would work however I don't think I have bound this correctly as none of the $accessKey, $secretKey, $userAgent and $domain are set whenever I try and do anything from within my controller.

Is this even possible or am I just going about it the wrong way?

Thanks in advance.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1Wle95b
via IFTTT

Aucun commentaire:

Enregistrer un commentaire