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

Code completion in you Views

Try this one:

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

anonymous user on 4/10/08

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

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

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

mark story on 8/10/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 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.

mark story on 9/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 16/10/08

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

mark story on 16/10/08

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

anonymous user on 16/10/08

how to make ‘Code completion’ work in Views?

I really don’t where to put

sopheak on 19/3/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 13/4/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 21/5/09

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

german on 5/7/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 10/7/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 17/3/11

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

Ezequiel on 24/1/12

does not work in view templates.

samir on 19/3/12

< prev12

Have your say:

*
* You can use Textile markup, but be reasonable