Code Completion for CakePHP in Eclipse

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.  * Post Model
  3.  *
  4.  * @var Post
  5.  */
  6.  var $Post;

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

Code completion in you Views

Try this one:

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

anonymous user on 10/4/08

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

anonymous user on 10/4/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.

anonymous user on 10/7/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.

mark story on 10/8/08

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

anonymous user on 10/9/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.

mark story on 10/9/08

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

anonymous user on 10/10/08

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

anonymous user on 10/16/08

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

mark story on 10/16/08

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

anonymous user on 10/16/08

how to make ‘Code completion’ work in Views?

I really don’t where to put

sopheak on 3/19/09

Thanks, good solution.
sopheak: you might create another project with the file code_completion_views.php and add it to your include_path.

danilo04 on 4/13/09

You could also use PHPEclipse has good autocompletion feature and more
Refer here on the steps on how to setup it for CakePHP

http://myleskadusale.wordpress.com/2009/04/30/my-cakephp-developers-tools/

Myles on 5/21/09

I want to code completion in eclipse with whole cakephp framework , to get references to clasess etc, how to it done?

german on 7/5/09

It does not work. In post.php (model) this is its code:

<?php
class Post extends AppModel
{

var $name = ‘Post’; /** * Post Model * * @var Post */ var $Post;
}
?>

In posts_controller.php $this->(dose not prompt Post) and $this->Post->(does not prompt prperties and methodes of the mother class AppModel.)

Said Bakr on 7/10/09

Stas – it’s too bad this post had to end in spam. code lobster won’t solve code completion issues in eclipse. Many folks who also try codelobster end up going to back to eclipse because the plug-ins are really minimalistic and seem to be a marketing cash grab. The ide is just ok in my opinion.

Terry on 3/3/11

Anyway to get $this->form instead of $form-> code completion in views in Aptana Php?

baker7 on 3/17/11

This doesn’t work for views when using $this->

Ezequiel on 1/24/12

does not work in view templates.

samir on 3/19/12

Comments are not open at this time.