dmag's avatar
Level 6

Singleton binding doesn't seem to work

I have a service provider that is registered with singleton binding:

namespace LaravelShipStation;

use Illuminate\Support\ServiceProvider;

class ShipStationServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->singleton(ShipStation::class, function ($app) {
            return new ShipStation(config('shipstation.apiKey'), config('shipstation.apiSecret'));
        });
    }
}

However when I call this class from application:

$shipStation = new ShipStation();

it produces the following error:

Type error: Too few arguments to function LaravelShipStation\ShipStation::__construct(), 0 passed, exactly 2 expected

0 likes
1 reply
rodrigo.pedra's avatar
Level 56

You should use the application container to retrieve your instance, new is a PHP language construct and Lumen does not monitor when you use that.

Try using this to get your instance:

$shipStation = app(ShipStation::class);

Check the docs about how the container resolves this kind of calls:

https://lumen.laravel.com/docs/5.5/container

1 like

Please or to participate in this conversation.