Nullable controller dependency injection

sephelps posted 6 months ago

I'm getting a Too few arguments exception when trying to inject two nullable services in a controller method:

    public function home(Ordering $ordering, Newsletter $newsletter)
        $this->data('ordering', $ordering);
        $this->data('newsletter', $newsletter);

        return $this->theme->view('home', $this->data);

When the Ordering and Newsletter classes are resolved, it's possible for it to return null. However, in order to type-hint the classes for injection, null is not allowed. Defaulting both of the variables to null returns null for both even if they are instantiated.

How do I type-hint or inject nullable classes into my controllers?

Sign in to participate in this thread!

We'd like to thank these amazing companies for supporting us