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.
- /**
- * Post Model
- *
- * @var Post
- */
- 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- <?php
- //Add in some helpers so the code assist works much better.
- if(false) {
- $ajax = new AjaxHelper();
- $form = new FormHelper();
- $html = new HtmlHelper();
- $javascript = new JavascriptHelper();
- $number = new NumberHelper();
- $session = new SessionHelper();
- $text = new TextHelper();
- $time = new TimeHelper();
- $pagination = new PaginationHelper();
- $rss = new RssHelper();
- $xml = new XmlHelper();
- $number = new NumberHelper();
- }
- ?>
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.
Search
Categories
Recent Posts
- Using bindModel to get to deep relations
- New home, new sideproject
- Using bitmasks to indicate status
- AclExtras Shell
- Getting a new Oven, Migrating from CakePHP 1.1 to 1.2
- Code Completion for CakePHP in Eclipse
- CakePHP RC3 released and CakePHP 1.1 new release
- Book Review: CakePHP Application Development
- Providing Contextual Form Help with Mootools
- Creating gracefully degrading javascript and enabling progressive enhancement
Comments
SiteX on 4/10/08
Code completion in you Views
Try this one:
/* @var $html HtmlHelper */ $html->…
?>
SiteX on 4/10/08
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?