Configure PhpStorm to Auto-complete CakePHP Models, Views, and Controllers
After playing around a bit today I finally figured out how to get PhpStorm to auto-complete methods for models and controllers. Here’s what you need to do.
Removing Multiple Definitions
First, let’s tackle the multiple definitions problem that we see below.
There are multiple places defining AppController. We need to remove the ones that are included in the following locations from our ‘Directories’ in the project’s settings.
The two locations are:
- $CAKEHOME/cake/console
- $CAKEHOME/cake/tests
Next we need to mark the following file as plain text.
- $CAKEHOME/cake/libs/controllers/app_controller.php
You should now see that PhpStorm is no longer complaining about multiple definitions. If it is you may want to check your plugins/components to see if they’re mucking it up. If they are, just mark the file with the definition as plain text.
Auto-completion should now work for the controller. However, it’s still not working correctly on our model.
Adding the Model
To fix the model we need to add a magic property to the class.
*@property ModelName $ModelName
*/
Here’s an example from the controller we’ve been working in.
We can now auto-complete on our models in the controller.
Defining Model Relationships
Lastly, we need to add magic properties to our models to define its relationships with other models. Basically, for each “belongs to” relationship defined in the model’s file you need to add the magic property comment.
We can now auto-complete these relationships.
Setting Up Helper Auto-completion in Views
To get auto-completion working in views we need to include a file created by junichi11 over at GitHub.
Download this file and save it in a directory somewhere outside of your current project. I did this so I could use the same file on multiple projects.
Now add that directory to your current project.
Open a view file and add the following variable definition.
*@var $this View
*/
You should now be able to auto-complete helpers in your view!
Core Component Auto-Completion
Add the following to your app_controller.php file and this will add component auto-completion.
* CakePHP Component & Model Code Completion
* @author junichi11
*
* ==============================================
* CakePHP Core Components
* ==============================================
* @property AuthComponent $Auth
* @property AclComponent $Acl
* @property CookieComponent $Cookie
* @property EmailComponent $Email
* @property RequestHandlerComponent $RequestHandler
* @property SecurityComponent $Security
* @property SessionComponent $Session
*/










Kyle Newsome
Well done, works like a charm. I wish there was a way to mark groups of folders as plain text in PHPStorm and this would be an even simpler task
Cheers
torsten edelmann
Great suggestion, but I have a strange effect:
I can use code completion for Helpers in views, but as soon as I have completed it the method is marked as an undefined method. For example:
$this->Html->l
will automcomplete to
$this->Html->link()
but link() will be marked as unknown method, even so PHPStorm showed it in the autocomplete dialog perfectly, even suggesting the necessary options.
You don’t by chance know where this stems from? I already filed a bugreport with Jetbrains, but Cake support is not high on their list.
Jeremy Frazao
Can’t thank you enough for this. Been working without autocomplete for much so long I’d basically resigned myself to life without it in CakePHP.
Seriously – an absolute godsend. Well done!
Adam Culp
I also had to mark $CAKEHOME/cake/libs/controllers/app_model.php as plain text to get inherited model methods to autocomplete properly in the Controllers.
zerkms
Why not use
/**
* @var FooBar
*/
public $propertyName;
syntax instead? It is a bit more readable, as long as you describe a property in-place, not somewhere on the top of the file. Also in this case you could specify some text about what data the property handles
Elte Hupkes
@torsten edelmann: I get the same problem, even created a ticket for it quite a while ago. Please upvote it in the issue tracker so that maybe we’ll have a chance of getting it fixed.