lundi 18 mars 2019

Laravel 5.7: prevent auto load injection in controller constructor

I have a HomeController with his constructor that takes a Guzzle instance.

   /**
     * Create a new controller instance.
     *
     * @param \GuzzleHttp\Client|null $client
     *
     * @return void
     */
    public function __construct(Client $client = null)
    {
        $this->middleware('auth');
        $this->middleware('user.settings');
        if ($client === null) {
            $param = [
                'base_uri' => 'http://httpbin.org/',
                'defaults' => [
                    'exceptions' => false,
                    'verify' => false
                ]
            ];
            $client = new Client($param);
        }
        $this->setClient($client);
    }

I would use via __constructor() to be able to mock it in tests. My issues is that Laravel automatically auto-load the injection and the Guzzle Client injected has blank defaults (and cannot anymore edit it). In other words: at first call of HomeController Client is not null. And I need as null.

How can I stop this behaviour (only for the __construct() for HomeController)? I really use the DI in every part of my webapp.

Thank you



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire