Skip to content
This is the documentation for pieceofcake2/bake, a CakePHP 2.x plugin for pieceofcake2/cakephp. About this fork.

Bake Documentation ​

The Bake plugin provides the bake console shell, which generates models, controllers, views, fixtures, tests, plugins and projects for CakePHP 2 applications running on pieceofcake2/cakephp. See also Code Generation with Bake in the CakePHP 2 book.

Installation ​

Install the plugin with Composer as a development dependency. Applications created from the pieceofcake2/app skeleton already have it.

bash
composer require --dev pieceofcake2/bake

The plugin has the cakephp-plugin type, so it is installed into your plugins directory by composer/installers. Set installer-paths in your application's composer.json so it is installed into the right place:

Layoutinstaller-paths
Modern (config/, src/, templates/, plugins/)"plugins/{$name}/": ["type:cakephp-plugin"]
Legacy (app/Config/, app/Plugin/)"app/Plugin/{$name}/": ["type:cakephp-plugin"]

Load the plugin in config/bootstrap.php (legacy layout: app/Config/bootstrap.php). If you already use CakePlugin::loadAll(), this is not necessary:

php
CakePlugin::load('Bake');

Bake is a development dependency, so it is not installed when you deploy with composer install --no-dev. Loading a plugin that is not installed throws a MissingPluginException, so only load it where it is installed, for example:

php
if (Configure::read('debug')) {
    CakePlugin::load('Bake');
}

Running Bake ​

Run Bake from the project root with bin/cake (legacy layout: app/Console/cake):

bash
# Bake a model
bin/cake bake model User

# Bake a controller
bin/cake bake controller Users

# Bake views
bin/cake bake view Users

# Bake the model, controller, views, fixture and tests of a model
bin/cake bake all User

# Bake a new plugin
bin/cake bake plugin MyPlugin

Running bin/cake bake (or a command such as bin/cake bake model) without a name starts an interactive mode that guides you through the generation. If database.php does not exist yet, Bake first offers to create it.

The files are written to the directories of your application, so a modern application gets src/Model/, src/Controller/ and templates/, and a legacy application gets app/Model/, app/Controller/ and app/View/.

Use --help to see the arguments and options of each command:

bash
bin/cake bake controller --help

Commands ​

CommandDescription
all [<model>]Bake the model, controller and views of a model, and its fixture and tests when PHPUnit is installed.
model [<name>]Bake a model with its validation rules and associations. model all bakes all models.
controller [<name>]Bake a controller. --public adds the CRUD actions (index, view, add, edit, delete), --admin adds them for one of the Routing.prefixes. controller all bakes all controllers.
view [<controller>] [<action>] [<alias>]Bake the views (index, view, add, edit) of a controller, or a single view.
fixture [<name>]Bake a test fixture. fixture all bakes the fixtures of all tables.
test [<type>] [<name>]Bake a test case for a controller, model, helper, component or behavior.
plugin [<name>]Bake the directory structure, AppController and AppModel of a new plugin.
db_configBake the database.php configuration file.
project [<path>]Bake a new application from a skeleton.

Names can be given as Plugin.Name to bake into a plugin, for example bin/cake bake model ContactManager.Contact.

bake project copies the legacy app/-style skeleton bundled with the plugin (Console/Templates/skel). Use --skel to copy another directory. To start a new application with the modern layout, use composer create-project pieceofcake2/app instead (see pieceofcake2/app).

Options ​

OptionCommandsDescription
--connection, -call, model, controller, view, fixtureDatabase connection to use (default: default).
--plugin, -pmodel, controller, view, fixture, testPlugin to bake into.
--theme, -tall commandsBake theme to use.
--force, -fmodel, controller, view, fixture, testOverwrite existing files without prompting.
--publiccontrollerAdd the CRUD actions.
--admincontroller, viewAdd the actions or views for the admin routing prefix.
--count, -nfixtureNumber of records to generate (default: 1).
--records, -rfixtureImport records from the live table (--count records, default 10).
--schema, -sfixtureImport the schema instead of writing it into the fixture.
--emptyprojectCreate empty files in each directory, for example to keep them in Git.
--skelprojectDirectory to copy the new application from.

Bake Themes ​

Bake generates the code from templates. The default templates are in the plugin's Console/Templates/default/ directory:

Console/Templates/default/
├── actions/
│   └── controller_actions.ctp
├── classes/
│   ├── controller.ctp
│   ├── fixture.ctp
│   ├── model.ctp
│   └── test.ctp
└── views/
    ├── form.ctp
    ├── index.ctp
    └── view.ctp

To customize the generated code, create a bake theme: a directory in src/Console/Templates/ of your application (legacy layout: app/Console/Templates/) or in Console/Templates/ of a plugin, with the templates you want to change. Copy them from the default theme and edit them:

src/Console/Templates/myTheme/
├── classes/
│   ├── controller.ctp
│   ├── fixture.ctp
│   └── model.ctp
└── views/
    ├── form.ctp
    ├── index.ctp
    └── view.ctp

A theme directory must contain at least one of the actions/, classes/ and views/ directories. Templates that a theme does not have are taken from the other installed themes and finally from the default theme.

Select the theme with --theme:

bash
bin/cake bake controller Users --theme myTheme

When more than one theme is installed and no theme is given, Bake asks which one to use.

See LICENSE for copyright and license information. Not affiliated with the Cake Software Foundation.