CakePHP support in NetBeans

Bookmark and Share

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.

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.

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.

43 comments on “CakePHP support in NetBeans

  1. Pingback: uberVU - social comments

  2. Pingback: Setting Up A New CakePHP Environment in Ubuntu | Andy Regan

  3. 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

    • 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:

      /* @var $this View */
      /* @var $html HtmlHelper */
      /* @var $javascript JavascriptHelper */
      .....
      

      This can be used in netbeans with any variable. Please notice this only works with netbeans 6.7 and 6.8.

  4. Pingback: CakePHP support in NetBeans | TipLite | Source code bank

  5. 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;

  6. error on that last post, the end should say @var Profile (not Author)
    class Author extends AppModel {
    /**
    * @var Profile
    */
    var $Profile;

  7. Pingback: How to highlight syntax in .ctp (Cake View) in NetBeans | WebDevelopment

  8. “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 ?????????????????

    • 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.

  9. 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.

    • 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.


  10. Mystic:

    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:
    /* @var $this View */
    /* @var $html HtmlHelper */
    /* @var $javascript JavascriptHelper */
    …..

    This can be used in netbeans with any variable. Please notice this only works with netbeans 6.7 and 6.8.

    is there any way to Components auto complete???


  11. Mystic:

    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.

    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

  12. 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

  13. Pingback: PHP Answers » Answers Archive » $html versus $this->Html (is it the same?)

  14. Pingback: $html versus $this->Html (is it the same?) | DEEP in PHP

  15. Pingback: CakePHP auto-complete in NetBeans « mironiasty is coding

  16. Pingback: CakePHP auto-complete in NetBeans | mironiasty is coding

  17. 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.

    • 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.

  18. I’ve found this isn’t working in a setup with CakePHP 2, setting public $Category; at the top of the controller seems to overwrite $this->Category and makes it unusable :/

  19. Just use the ‘cake bake’ from the console. CakePHP will automatically generate those comments for you and then include Cake library from Net Beans project. Done!

    • Thank you @Walter but this works only since Cake2 and some developers, including myself, are stuck with Cake 1.2 or 1.3 which don’t generate those comments out of the box

      • I tend to agree. Another argument bdieses the title importance is the fact that most of the times, if you want to construct a search engine friendly and relevant title, you often use the elements processed in the controller, so it’s only natural you set the title in that environment, not construct it someplace else.By the way, you mentioned frameworks and such, I set my mind on trying CodeIgniter sometime soon. Other than manually testing each of the frameworks I’ve set my mind on tackling, I could use an advice. Cake, Zend, CI or something else?

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>