Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

NielsNumbers's avatar

How do you declare return type View in controller?

I would like to type hint my controller methods like this:

public function index(): \Illuminate\View\View;
{
        $users = User::orderBy('created_at', 'desc')->paginate(30);

        return view('logs.users.index', compact('users'));
}

Although this works, my PHPStorm editor will complain with this:

Return value is expected to be '\Illuminate\View\View', '\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View' returned

I would need to change it to

public function index(): \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
    {
        $users = User::orderBy('created_at', 'desc')->paginate(30);

        return view('logs.users.index', compact('users'));
    }

To keep my IDE happy. But I feel like this looks really messy. Especially as I have to do it for every controller method.

How are you doing it? Is there a better alternative? Or do you just suppress those kinds of warnings?

1 like
7 replies
Sinnbeck's avatar

Doesn't is work with just

:\Illuminate\Contracts\View\View

If so you can import it

use Illuminate\Contracts\View\View;

public function index():  View
tykus's avatar

How are you doing it?

Not. There is nowhere in my own code where I would directly use the result of a Controller action, so typehinting is not useful or relevant.

The IDE complaint results from the return value of the view helper function which is those union types. Instead, you could use the View facade instead - View::make returns \Illuminate\Contracts\View\View only? Or response()->view() which only returns \Illuminate\Http\Response?

2 likes
NielsNumbers's avatar

@tykus yes agree, it's not relevant. But I like to have a return type check on my IDE, so I do not want to suppress the warning. But having an error wave behind every return statement in my controller is also not acceptable for me. Does it mean you have disabled this warning category in your IDE?

Yes, using the VIew facade get rids of the error. Thanks for that.

tykus's avatar

@Elenktik I just do not include a return typehint; like I said it it not relevant to the code I am writing.

NielsNumbers's avatar

@tykus Yes I understand that you don't set any return type on your controller method, but I wonder how you handle the warnings.,Maybe I was making too many assumptions. I supposed that you

  • use PHPStorm
  • use PHPStan validation rules to check your code

Is that the case? If so, I was wondering, if you surpress the warning "Incompatible return type" or just "Missing return type declaration"? Or are you seeing lots of warnings in your IDE if you go to the controller? Or is there any possibiliy to have both warnings on but just disable it for controllers?

enter image description here

jonnyhocks's avatar

This is what I use:

use Illuminate\View\View;

public function index(): View
3 likes

Please or to participate in this conversation.