How to not lose the autocomplete magic in your MCV views.

Like most MCV frameworks the controller passes the variable to the view.

Something like this in codeigniter:

$data["user"] = User::find($id);
$this->load->view('user/profile', $data);

Or Laravel:

$data["user"] = User::find($id);
return View::make('user.profile', $data);

Now if we would try to get some autocomplete the $user var in view we get nothing.

autocomplete, no suggestion

Was it getName(), getSurname() … no I’m pretty sure it was getLastName()..Ah we’ll need to look it up in the Class.

What! But Why? I’ve paid good money for this IDE and it’s awesome autocomplete function, why doesn’t it work??

First of all, don’t blame your IDE dummy. How would it know wat those resources are? It doesn’t.

Now heres the magic how it does:

Just add the @var PHPDoc to your view, just as you would in a Class.

/*@var $user User*/

No more “no suggestions”.

Whoohoo it works!

Whoohoo it works!

PS. MOST IDE will make the magic happen, the only one I’ve tested which doesn’t is Netbeans. Be free to add more in comments.

Tagged , , ,

Leave a Reply