Code Completion for CakePHP in Eclipse

Oct 04 2008

Eclipse is widely popular, robust and powerful IDE. It supports PHP through the PDT project . PDT gives you some good PHP related development tools, including code completion & code templates to help you save some time. I use eclipse quite a bit for my day job, so here are somethings I do to make my life easier and productive.

Code completion for models and components

Code completion in eclipse is dependent on the project builder figuring out what is what. This works great for $this-> methods. It falls short when you want to get code completion for Models or Components though. To enable code completion for Models and Components do the following for each Object you want code completion for in your controller.

Show Plain Text
  1.  
  2. /**
  3.  * Post Model
  4.  *
  5.  * @var Post
  6.  */
  7.  var $Post;
  8.  

This will let eclipse know that at $this->Post you expect an instance of your Post class. You can enable code completion for components in a similar fashion.

Code completion in you Views

Enabling code completion for for helpers in you view is equally easy. I have a file that I place in the project somewhere (you could also put it in your Include Path ). This file simply makes an instance of each helper I want to have code completion for.

Show Plain Text
  1. <?php
  2. exit();
  3. //Add in some helpers so the code assist works much better.
  4. if(false) {
  5.     $ajax = new AjaxHelper();
  6.     $form = new FormHelper();
  7.     $html = new HtmlHelper();
  8.     $javascript = new JavascriptHelper();
  9.     $number = new NumberHelper();
  10.     $session = new SessionHelper();
  11.     $text = new TextHelper();
  12.     $time = new TimeHelper();
  13.     $pagination = new PaginationHelper();
  14.     $rss = new RssHelper();
  15.     $xml = new XmlHelper();
  16.     $number = new NumberHelper();
  17. }
  18. ?>

the exit and if(false) are ignored by eclipse and prevent the script from ever executing.

Code Templates

Eclipse also supports code templates so you can alleviate some of the boring repetition that comes from typing $form->input() a million times. I’ve made set of templates for both my views and controllers. You can find them along with the view code completion file at my github

I’ve included templates for the things I find myself doing the most often. If there are others you would like to see let me know, or fork the repository and add them in.


Comments

SiteX on 4/10/08

Code completion in you Views

Try this one:

/* @var $html HtmlHelper */ $html->…
?>

SiteX on 4/10/08

<?
/* @var $html HtmlHelper */
 $html->...
?>

raph on 7/10/08

How to use html templates in ctp files? I have a problem with this because in ctp files only PHP templates works for me.

mark story on 8/10/08

raph: For me when I’m not inside a <?php ?> block the html templates trigger, since all the templates I posted have <?php ?> I posted them as html templates, but I think you should be able to import them into the PHP templates. By the way I’m just using the PHP/default HTML editor for .ctp/.thtml files.

dr. Hannibal Lecter on 9/10/08

Just a note, the same should work in most cases for Zend Studio too (at least in the old non-eclipse version).

mark story on 9/10/08

dr. Hannibal Lecter: Good to know, all the snippets will work in the current versions of Zend Studio as well, as they are sibling projects.

Raph on 10/10/08

Mark: everything it’s ok – my mistake. :)

gravyface on 16/10/08

Can get the models working, but not the components. Trying it with Auth as such:
/** * AuthComponent * * @var Auth */ var $Auth;

mark story on 16/10/08

gravyface: Try using @var AuthComponent instead. That is the proper classname.

gravyface on 16/10/08

That did it. Thanks, Mark. Great lookin’ site btw.


Have Something to say?

*
* (Never published, I promise)
* You can use Textile markup, but be reasonable

Recent Artwork

  • CakePHP Test suite icons
  • Shuriken 2
  • Clumsy Penguins
  • Balloon Animals
  • Metal in the air! (vertical)
  • Cleavers