CakePHP support in NetBeans
NetBeans is a very powerful development environment for many languages including PHP. With the release of its latest version, NetBeans announced full support for symfony PHP framework. NetBeans can also support many other PHP frameworks and libraries although it is not officially supported by NetBeans. This support needs some effort to enable some tools specially auto-complete, or intellisense, which saves a lot of time for developers.
We will start to make NetBeans support CakePHP. Firstly we need to create a cake project using cake command line tools.
cd DOCUMENT_ROOT cake bake PROJECT_NAME
Then we need to create a NetBeans project using the create files. Click file > New Project, then choose New Project. From the window choose PHP from categories and from projects choose PHP Application with Existing Sources. Click Next then choose the project path. and click finish.
After the project is created select the project in the Projects pane and right click Include Path. Click add folder and choose cake library path and add it. NetBeans will scan and add the library to its internal library.
Now you will have auto-complete working in your controllers, models, controllers, behaviors and helpers files. This will happen because NetBeans is smart enough to know that your inheriting AppController, AppModel,...etc which in turn inherits Controller and Model classes which is found in CakePHP library directory which NetBeans just scanned. But till now we don't have support for models and components included magically by CakePHP in controllers and models. To make NetBeans identifies those variables we will use PHPDoc trick. In your model or controller add a variable with the same name of your model or any other model you want, but make sure it is loaded as this will not include your model.
class ProductsController extends AppController{
var $name = 'Products';
/**
* @var Product
*/
var $Product;
/**
* @var EmailComponent
*/
var $Email;
}
You will notice that NetBeans enables auto-complete even in comments. Just hit Ctrl + Space when writing the name of any class. This will work with associated models in a model class.
Now you will notice that your CakePHP work is much more easier, but we still have auto-complete is not working in views. This problem can be solved using NetBeans feature which enables us to define a variable's type using comments. I call this feature var-doc or variable type notice. Just write a comment like this before any variable. You even can make this with $this variable to define the class you are working with as View class.
/* @var $this View */ /* @var $html HtmlHelper */ /* @var $javascript JavascriptHelper */ .....
This way you can have auto-complete support for your views and helpers included in views and you will notice that NetBeans also has support for your classes in comments.
At last the only thing that I couldn't have support for is behaviors methods which can be called from a model's instances if you now a way to do this please tell me. But we are waiting for more from NetBeans which has introduced too much for us, PHP Developers.


February 17th, 2010 - 00:03
Thanks for the tip, Mystic, it’s very good to know that I can enable auto complete for Views. By the way, this works for CodeIgniter as well.
February 17th, 2010 - 19:46
Thanks for the tip! Now it’s a lot easier to write code with NetBeans using cake
February 22nd, 2010 - 10:46
nice article
March 2nd, 2010 - 10:55
well written, thanks for the tips!
March 12th, 2010 - 05:04
hi Hazeim Regards from Peru, great idea about auto-complete code by using Netbeans… but I’m facing a problem how should I work with the helpers? Thanks a lot
March 12th, 2010 - 10:36
Hi,
It is so simple, just use the trick I mentioned in the article. Just write a comment like the following for the helper you want to use in your view file:
This can be used in netbeans with any variable. Please notice this only works with netbeans 6.7 and 6.8.
March 15th, 2010 - 18:05
That could be nice to now lastly look for a websites where the blogger definitely understands what he’s sharing.
April 22nd, 2010 - 17:31
You can turn on code suggest for related Models in your controller code also. For example, my $Post model belongsto an $Author and I want to be able to access the $Author model through my $Post model in my posts_controller like this:
$this->$Post->$Author->find(…
In your $Post model add the variable:
class Post extends AppModel {
/**
* @var Author
*/
var $Author;
Code suggest will now work for the related Models. Even better, is that if you are in your posts_controller and want to access a relationship of the $Author like hasone $Profile, like $this->$Post->$Author->$Profile, you don’t have to do anything extra, just make sure you have defined the related model in your $Author model like this:
class Author extends AppModel {
/**
* @var Author
*/
var $Profile;
April 22nd, 2010 - 17:33
error on that last post, the end should say @var Profile (not Author)
class Author extends AppModel {
/**
* @var Profile
*/
var $Profile;
June 30th, 2010 - 03:02
“After the project is created select the project in the Projects pane and right click Include Path. Click add folder and choose cake library path and add it. NetBeans will scan and add the library to its internal library.”
IN THE ABOVE LINE WHERE IS “choose cake library path” ????????????????????????
please i need the answer
is that c:/wamp/cake/cake/console ?????????????????
June 30th, 2010 - 20:15
The cake path to make it work right is c:/wamp/cake/cake/ or more accurately c:/wamp/cake/cake/libs. The path you provided is for cake baker which should be in you PATH environment variable.
July 2nd, 2010 - 18:36
Tried, but netbeans seems to freeze on the file load — great idea now to see where I messed up at! Thnx for the information!
July 17th, 2010 - 06:48
is there any way to add auto completion for Components and helpers???
i am not getting the suggestions when using $form or $this->Email->
@tnhomestead
it freezes for me too. then i quit the whole netbeans and tried again that time its worked fine. don’t know the actual reason.
July 17th, 2010 - 10:08
Please read the article again and you will find your questions answered. For the freeze please tell me you version it is recommended to use a version after 6.7.
July 17th, 2010 - 08:13
is there any way to Components auto complete???
July 17th, 2010 - 11:12
why this not working??
/* @var $Email EmailComponent*/
var $Email;
freezing problem i found in 6.8 but now i upgraded to 6.9 and free from that problem
July 26th, 2010 - 22:48
Thanks, finally I can use code suggestions in Netbeans for CakePhp.
August 4th, 2010 - 11:04
Am i right in thinking that it is the documentation variables that trigger the auto-completion?
I always wanted to use the proper doc tags but never could be bothered, this is a good enough reason to bother though!
Nice one thanks
August 6th, 2010 - 18:40
Well I guess it’s the same thing, but written in a different way, this didn’t worked very well for me, until I read this
http://bakery.cakephp.org/articles/view/model-based-code-insight-and-completion-in-netbeans
August 6th, 2010 - 19:08
I already read this article. It works with NetBeans 6.5 but bot next versions.
October 26th, 2010 - 17:03
Does it work with 6.9 version? Thanks
October 26th, 2010 - 18:57
Yes it works on any version >= 6.7
May 20th, 2011 - 07:13
how to use this tips in netbean 7.0 plz help me!!!
May 20th, 2011 - 22:06
It is the same for all netbeans versions
July 31st, 2011 - 03:29
How can I make the code completion work in views for $this->Html->…
Your code only works for $html
September 27th, 2011 - 18:29
This will be found on one of my next articles keep watching
November 1st, 2011 - 15:47
Anyone knows what folder to point at when using Linux Mint/Ubuntu? Installed Cake 1.3 from Software Manager. Tried /usr/share/php/cake and /usr/share/php/cake/lib and some other variations but it does not seem to do the trick.
November 1st, 2011 - 22:46
On my Ubuntu 11.10 installation it is on /usr/share/php/cake and it is working I think it will be the same for older distros too. Sometimes Netbeans does a scanning project and it takes a while to be completed. Other times it completely stops then you can restart it and it will work correctly.