AutoHotkey Guide for CakePHP

This article is also available in the following languages:
By Tr0y
Tired of typing "class YourModelsController extends AppController..." over and over? If so this, is for you.
NOTE: This guide assumes you are using PHPeclipse as your IDE of choice. If you use another IDE or notepad you will have to adjust the AutoHotkey script accordingly.

Step 1

Download and Install AutoHotkey ( it's free ) - http://www.autohotkey.com/

Step 2

Edit the default script to look like the following.


;-----------------------------------------------------------------------------
;    CakePHP
;-----------------------------------------------------------------------------

;model
:c*?:]cmod::
InputBox, MyModel, Model Name Prompt, Enter model name ( e.g. MyModel ),,250,125
if ErrorLevel=0
{
    BlockInput On
    Send, class %MyModel% extends AppModel{enter}{{}{enter}var $name = '%MyModel%';{tab}// required for php4 installs{enter}var $displayField = 'id';{enter}
    BlockInput Off
}
return

;controller
:c*?:]ccon::
InputBox, MyModels, Models Controller Prompt, Enter controller name ( e.g. MyModels ),,250,125
if ErrorLevel=0
{
    BlockInput On
    Send, class %MyModels%Controller extends AppController{enter}{{}{enter}var $name = '%MyModels%';{tab}// required for php4 installs{enter}var $scaffold;{enter}
    BlockInput Off
}
return


Step 3

Fire up PHPeclipse and create a model php file.

Reload the script by right-clicking the taskbar icon and reload.

type ]cmod

It should prompt you for the model's name, so type your models name.

For controllers type ]ccon

Comments

  • Posted 11/08/07 11:59:54 PM
    Have a look at http://gennit.com it does code generation for CakePHP, and creates the MySQL database as well.
  • Posted 03/14/07 09:20:31 AM
    you can define shortcuts like mentioned above in eclipse so you do not need autohotkey. just go to the templates in PHPeclipse user settings and create your own templates, e.g.

    /**
     * @author ${user}
     * @since ${date}
     */
    class ${primary_type_name} extends AppModel {
        var $$name = '${primary_type_name}';
        
        var $$hasMany = array();
        var $$belongsTo = array();
        var $$hasAndBelongsToMany = array();
        
        ${cursor}
    }

    when you create a new model just type 'model' and press ctrl + space and eclipse inserts the model code. ${primary_type_name} inserts the filename without extension so there is almost nothing to do left :-).
  • Posted 11/17/06 08:33:31 PM
    For Vim users you can get the snippetsEmu script to have snippets just like Textmate :-)

    http://www.vim.org/scripts/script.php?script_id=1318
  • Posted 10/30/06 02:22:49 AM
    ^ Yea, I'd be interested in some snippets.

    :-)
  • Posted 10/26/06 04:00:09 PM
    Or for none-mac users, you can use SCiTE -- which is a VERY handy editor.

    It has the same snippets functionality as Textmate and they are user-definable with an easy-to-understand syntax. Just define a set of snippets you find valuable and it's just a keyword and ctrl-b away.

    If anyone is interested, I can post my snippets file (known to SCiTE users as an abbreviations file).
  • Posted 10/03/06 08:27:47 AM
    if you're on a mac, just try out textmate (macromates.com) and its bundles.

    there is one for cakephp to do things like this automatically.

Comments are closed for articles over a year old