Keep It Simple Smart
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.
| Print article | This entry was posted by tiplite on 12 February, 2010 at 5:22 pm, and is filed under CakePHP, PHP, netbeans. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |


about 6 months ago
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.
about 6 months ago
Thanks for the tip! Now it’s a lot easier to write code with NetBeans using cake
about 6 months ago
nice article
about 6 months ago
well written, thanks for the tips!
about 5 months ago
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
about 5 months ago
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.
about 5 months ago
That could be nice to now lastly look for a websites where the blogger definitely understands what he’s sharing.
about 4 months ago
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;
about 4 months ago
error on that last post, the end should say @var Profile (not Author)
class Author extends AppModel {
/**
* @var Profile
*/
var $Profile;
about 2 months ago
“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 ?????????????????
about 2 months ago
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.
about 2 months ago
Tried, but netbeans seems to freeze on the file load — great idea now to see where I messed up at! Thnx for the information!
about 1 month ago
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.
about 1 month ago
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.
about 1 month ago
is there any way to Components auto complete???
about 1 month ago
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
about 1 month ago
Thanks, finally I can use code suggestions in Netbeans for CakePhp.
about 1 month ago
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
about 4 weeks ago
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
about 4 weeks ago
I already read this article. It works with NetBeans 6.5 but bot next versions.