Thursday, May 3, 2012

How to add cakePHP auto-complete to 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.
1cd DOCUMENT_ROOT
2cake 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.
NetBeans new project dialog
NetBeans New Project Dialog
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.
NetBeans PHP include path dialog
NetBeans PHP include path dialog

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.
01class ProductsController extends AppController{
02    var $name = 'Products';
03 
04    /**
05     * @var Product
06     */
07    var $Product;
08 
09    /**
10     * @var EmailComponent
11     */
12    var $Email;
13}
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.
1/* @var $this View */
2/* @var $html HtmlHelper */
3/* @var $javascript JavascriptHelper */
4.....
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.

No comments:

Post a Comment