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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s