Faster baking of controllers with the bake shell script

Published on and tagged with bake  cakephp  controller

In a previous post I wrote about a shortcut to make the baking a bit faster. In the meantime new shortcuts have been added. In this post I will describe the shortcuts which make the baking of controllers faster.

In the aforementioned article I described that you could use the following statement to bake a controller:

cake bake controller

From this statement the next logical step is to provide a way to define the name of a controller via command line:

cake bake controller Users

This command will generate the following controller:

// app/controllers/users_controller.php
class UsersController extends AppController {
    var $name = 'Users';
    var $scaffold;
}

This may be useful in certain cases, but usually you want to bake the actions, too. To allow that, you have to provide the “scaffold” keyword after the controller name:

cake bake controller Users scaffold

This generates the UsersController with the default implementations for the actions add(), delete(), edit(), index(), and view().

By adding the “admin” keyword, you can also generate the admin functions admin_add(), admin_delete(), admin_edit(), admin_index(), and admin_view():

cake bake controller Users scaffold admin

To generate only the admin functions you have to provide “null” instead of “scaffold”:

cake bake controller Users null admin

I like those shortcuts, they make the baking process a much better experience (I don’t like the question-driven approach). For models there are no such shortcuts available yet, but I am sure they come soon.

Happy baking :)

(thanks to gwoo for his short introduction)

Btw: If you have trouble with setting up the console, have a look at the respective screencasts.

21 comments baked

  • Paul

    very handy!

  • m3nt0rs weblog

    CakePHP 1.2 – Die neue Konsole unter Windows…

    Okay.. habe gerade den neusten Trunk von CakePHP 1.2 ausgecheckt und als erstes fiel mir auf das der “scripts” Ordner verschwunden ist. Es gibt nun einen neuen Ordner unter “cake” namens “console”. Dieser beherbergt …

  • MercedesAMG

    Yes these are short cuts. I just cant believe in a GUI world y we need to be dealing with command line in the first place.

    Cake is bringing up and finding the db tables anyway, a simple grid based check box goooooeeeee should do the trick
    ass a column for scaffolding and such like. It is all so repetitive.

    Mind U i’m a user learning PHP etc, so my intuitive approach may be way to simplistic for REAL programmers to accomplish

    just a thought.
    maybe there is a better place to discuss this anywhoo.
    cake is great anyway

  • cakebaker » Baking views

    […] Faster baking of controllers with the bake shell script I showed you some shortcuts for baking controllers. In this article I am going to show you […]

  • cakebaker

    @MercedesAMG: Sure, it would be nice to have a GUI, but I also think that it would require (too?) much effort to create a GUI which works on different platforms.

    But feel free to start a GUI bake ;-)

  • tclineks

    Here’s the start of a bash_completion script for cake’s console:

    http://bin.cakephp.org/saved/21917

    I took the approach of not modifying console/bake but with some listing interface to the available methods/tasks you could beef up the completion.

  • cakebaker

    @tclineks: Hm, what does this script do?

  • tclineks

    It gives you bash completion (through hitting tab) for shells and for the bake it gives you bake actions and will autocomplete potential model/view/controller names.

    Here’s an intro to bash_completion: http://www.debian-administration.org/articles/316

    So,
    after I source this (`. bashcompletion`, `source bashcompletion`, or link it in the right location -> /etc/bash_completion.d/) I can type

    `/cake [tab][tab]`
    and it lists the potential shells to use e.g. acl, api, bake (even app-specific shells)

    `/cake b[tab]` -> completes ‘bake’

    `/cake bake m[tab]` -> completes ‘model’

    If I further do
    `/cake bake model M[tab][tab]` it’ll complete for models beginning with ‘M’

    It’s pretty crude atm — doesn’t handle -app params (you can invoke the console within the custom app dir for now).
    If bake.php/shell is enhanced to support public, noninteractive listing this could be pretty sweet.

    cheers,
    Travis

  • tclineks

    Whoops, it stripped some explanatory stuff before the cake calls

    `/cake [tab][tab]`
    should be
    `(path to console)/cake [tab][tab]`

  • cakebaker » Bash completion script for CakePHP’s console

    […] Cline (aka tclineks) posted in a comment to the article “Faster baking of controllers with the bake shell script” a link to a bash completion script he started to write for CakePHP’s […]

  • Christian

    Anyhow, to continue with MercedesAMG Question.
    Is there a way to use bake via a web interface (e.g. on a remote server, where no console is available?)

  • cakebaker

    @Christian: Well, you could create a controller which uses shell_exec to execute bake on a remote server. But I don’t see why you would want to use bake in such a way as you usually develop on your local machine.

  • Christian

    @cakebaker:
    Think of a scenario, where people dont have local machines/account usually available (e.g. a school), but instead can develop using a browser.
    For them it would be perfect, if they see bake and its tasks in a web interface. The ruby guys give you a web version of a typing console input just for having a web tutorial.
    If understand the shell_exec command, but how do i repost bakes output into a web like frontend?

  • Christian

    @cakebaker:
    Think of a scenario, where people dont have local machines/account usually available (e.g. a school), but instead can develop using a browser.
    For them it would be perfect, if they see bake and its tasks in a web interface. The ruby guys give you a web version of a typing console input just for goint through a web tutorial.
    If understand the shell_exec command, but how do i repost bakes output into a web like frontend?

  • cakebaker

    @Christian: Well, shell_exec returns the output of the command executed in the console. But to make it user-friendly it probably takes quite some effort.

    Maybe the project AydCodegen (http://codegen.devayd.com/) is something for you, as its goal is to create a HTML frontend for bake tasks, but it is in a early development state.

  • foxmask

    As an example of GUI, CodeCrafter for CodeIgniter (a Framework with MVC too)
    http://codeigniter.com/forums/viewthread/47524/
    works fine
    May it could be tested to see how its working.

  • cakebaker

    @foxmask: Thanks for your comment!

    I agree with you that CodeCrafter could be used as an example by those who want to implement such an application for CakePHP.

  • cakebaker » Faster baking with bake

    […] have a look at Faster baking of controllers with the bake shell script and Baking […]

  • Baking plug-ins - cakebaker

    […] a UsersController with var $scaffold set. See Faster baking of controllers with the bake shell script or Baking views for more […]

  • obuwie ochronne

    good article

  • bilard

    Great and excellent article it’s realy helpful. Thanks again.

Bake a comment




(for code please use <code>...</code> [no escaping necessary])

© daniel hofstetter. Licensed under a Creative Commons License