Pages

Wednesday, August 24, 2011

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.

Multiple Definitions


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
Exclude Directories


Next we need to mark the following file as plain text.
  • $CAKEHOME/cake/libs/controllers/app_controller.php
Mark as Plain Text


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.

Multiple Definitions Resolved


Auto-completion should now work for the controller. However, it's still not working correctly on our model.

Autocomplete works, but not on the 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.

@property ModelName $ModelName


 We can now auto-complete on our models in the controller.

Auto-complete on the model.


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.

Define belongs to relationships


We can now auto-complete these relationships.

Auto-complete model 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!

Auto-completion of helpers


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  
  */  

31 comments:

  1. 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

    ReplyDelete
  2. 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.

    ReplyDelete
  3. 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!

    ReplyDelete
  4. 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.

    ReplyDelete
  5. 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

    ReplyDelete
  6. @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.

    ReplyDelete
  7. Looks totally awesome and can't wait to get it to work but since they reorganized all the files in CakePHP 2.1 I'm having some issues. Might you be so kind to update this for the paths in 2.1?

    Pretty please!

    ReplyDelete
  8. Sadly, I have not started any CakePHP 2.x development yet. I'll be sure to update if I get the opportunity.

    ReplyDelete
  9. @Dan Berlyoung
    CakePHP 2.1

    o Directories to remove: lib\Cake\Console, lib\Cake\Test
    o Controller: @property ModelName $ModelName
    o Model (Relationships): @property ModelName $ModelName
    o View: @var $this view
    o Integrate this file into your project: https://gist.github.com/934219
    e.g. app\Lib\cake_helper_code_completion.php

    Et Voila ;)

    ReplyDelete
  10. I am having a problem getting it to work on auto-completing the methods. If I type $this->A in my controller it will show Acl and Auth and that they are of type AuthComponent and AclComponent but when i continue and type $this->Auth-> it will not give me any hints even if I press ctrl space. if I then put $this->Auth->startup() it will give me an error saying that startup has a required parameter. So it does understand what type the component is but it refuses to autocomplete its methods for me... Anyone else run into this problem?

    ReplyDelete
  11. I remember I had it working before - now tried to set it up again but with more recent version of Cake 2.1.3 and PhpStorm 4 - fails somehow. $this->Acl-> (CTRL+SPACEBAR) brings up empty suggestion box, and loading indicator to the right. In Netbeans everything worked right from the startup oO.

    ReplyDelete
  12. After a bit more testing I found that the "Adding the Model" part is not working. Auto-completing models doesn't seem to work - at least not in PHPStorm 4 and CakePHP 2.1. Many people would be thankful if some PHP Pro could solve that for us ;) Thanks!

    ReplyDelete
  13. hey, just skip step no.2 "Next we need to mark the following file as plain text. $CAKEHOME/cake/libs/controllers/app_controller.php" .

    its workin fo me :)
    thanks dude :D

    ReplyDelete
  14. Awesome. Thank you very much for this information!

    ReplyDelete
  15. Anyone tried it on Aptana?

    It works fine with Controllers but I can't make it work with Views.

    I have the code:
    /**
    * @var View
    */
    $this;

    but there appears no autocomplete for $this value.

    ReplyDelete
  16. I cant make it work with custom or overriden components. For example I have a CustomSessionComponent in app/Controller/Components, added
    @property CustomSessionComponent $Session
    to the AppController but i still get the autocompletion to the original SessionComponent. Is there something I am missing or a fix for that?

    ReplyDelete
    Replies
    1. Previously I added the comment section at the start of the file and had some App::uses() between them and the class declaration. Adding it right before the class declaration fixed it. My mistake, sorry!

      Delete
  17. Thank you so much!

    You just saved me a lot of work hours I would accumulate by navigating to my Models each time I needed to check a method's signature.

    Nice one!

    ReplyDelete
  18. if you use containable behavior, do you know how you can get phpstorm setup to think ->contain exists?

    ReplyDelete
  19. I prefer to use Codelobster PHP Edition
    It's special CakePHP plug-in work much better for me

    ReplyDelete
  20. If you go through the insurance policy you might save yourself from a number of mishaps that happen commonly on Arkansas. autotransportbrokerleads.com/

    ReplyDelete
  21. But, to find the best auto insurance in Arkansas is a difficult task to achieve. In order to get to the best auto insurance in Arkansas you need to study about some of Arkansas auto insurance companies so that you will be able to determine whether which serves best.car transport

    ReplyDelete
  22. One great thing is that simply by looking into the different companies that are around, you will already learn a lot of about methods of transportation.vancouver auto transport

    ReplyDelete
  23. There are also new methods for transporting cars. Vehicle trailers, for example, have become a choice by a lot of customers. More Info

    ReplyDelete
  24. It is pretty standard throughout the phpstorm to treat the unusual as the removing multiple definitions. The BMW Export UK effortlessly takes the pole position for the future of luxury travel.

    ReplyDelete
  25. Miami named 'the Magic City' due to it's rapid growth is in the South-East of Florida and has a wonderful true tropical climate with hot, humid summers, and warm, dry winters. Luxury Car Rental Miami

    ReplyDelete
  26. This site have particular software articles which emits an impression of being a significant and significant for you individual, able software installation.This is the spot you can get helps for any software installation, usage and cracked.
    https://crackexe.net/https://crackexe.net/
    phpstorm-crack
    malwarebytes-premium-crack
    apowerrec-crack
    garmin-basecamp-crack
    beecut-crack
    netbalancer-crack
    debut-video-capture-crack
    openshot-video-editor-crack

    ReplyDelete