CakePHP 1.3 helper auto-complete in netbeans

Bookmark and Share

CakePHP 1.3 introduced a new way to use helpers. This was added to prevent confusion between helper variables and local variables added to the view file. You can access helper methods in a view by using $this->Helper->method(). This way make a problem if you want to use auto-complete for helpers introduced in a previous article

With a small trick you can make netbeans supports this method auto-complete. To do this add a file in your project folder, or preferable to be in CakePHP core folder, with a name dummy_view.php for example. Now add the following code to this file.

<?php

class DummyView extends View {

    /**
     * @var HtmlHelper
     */
    public $Html;

    /**
     * @var JsHelper
     */
    public $Js;

    /**
     * @var AjaxHelper
     */
    public $Ajax;

    /**
     * @var JavascriptHelper
     */
    public $Javascript;

    /**
     * @var FormHelper
     */
    public $Form;

    /**
     * @var SessionHelper
     */
    public $Session;

    /**
     * @var TextHelper
     */
    public $Text;

    /**
     * @var PaginationHelper
     */
    public $Paginator;

    /**
     * @var RssHelper
     */
    public $Rss;

    /**
     * @var XmlHelper
     */
    public $Xml;

    /**
     * @var CacheHelper
     */
    public $Cache;

    /**
     * @var TimeHelper
     */
    public $Time;

}

Now put the following line of code in end of your view file:

<?php /* @var $this DummyView */ ?>

Now you’ll see that netbeans displays auto-complete for those helper methods and view methods too like $this->element() for example.

CakePHP 1.3 helper auto complete in netbeans

Autocomplete for CakePHP 1.3 helpers in netbeans

How does this work?

The idea here is simple we tell netbeans that there is a new class that extends View class. Then when in view file we tell netbeans to consider that $this has the type DummyView. CakePHP run the code in view files inside render() method in View class so $this is really an instance of View class. Now as netbeans knows the type of $this variable and knows the types of its variables, which are CakePHP helpers, it shows their autocomplete options.

Finally, you should be aware of the following:

  • Make sure to include your CakePHP core path in netbeans include path or it is inside your project source folder.
  • This will not load helpers in run time, so make sure you include required helpers in your controller.
  • You can add your own custom helper in DummyView class using the same method as core helpers.

I know this has been late but it is still useful :)


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>