Extending Bake
As a view class, emits the same events as any other view class,plus one extra initialize event. However, whereas standard view classes use theevent prefix “View.”, BakeView
uses the event prefix “Bake.”.
The initialize event can be used to make changes which apply to all bakedoutput, for example to add another helper to the bake view class this event canbe used:
If you want to modify bake from within another plugin, putting your plugin’sbake events in the plugin config/bootstrap.php
file is a good idea.
Bake events can be handy for making small changes to existing templates.For example, to change the variable names used when baking controller/templatefiles one can use a function listening for Bake.beforeRender
to modify thevariables used in the bake templates:
- <?php
- // config/bootstrap_cli.php
- use Cake\Event\Event;
- use Cake\Event\EventManager;
- EventManager::instance()->on('Bake.beforeRender', function (Event $event) {
- $view = $event->getSubject();
- // Use $rows for the main data variable in indexes
- if ($view->get('pluralName')) {
- $view->set('pluralName', 'rows');
- }
- if ($view->get('pluralVar')) {
- $view->set('pluralVar', 'rows');
- }
- // Use $theOne for the main data variable in view/edit
- if ($view->get('singularName')) {
- $view->set('singularName', 'theOne');
- }
- if ($view->get('singularVar')) {
- $view->set('singularVar', 'theOne');
- }
- });
You may also scope the Bake.beforeRender
and Bake.afterRender
events toa specific generated file. For instance, if you want to add specific actions toyour UsersController when generating from a Controller/controller.twig file,you can use the following event:
- <?php
- // config/bootstrap_cli.php
- use Cake\Event\Event;
- use Cake\Event\EventManager;
- use Cake\Utility\Hash;
- EventManager::instance()->on(
- 'Bake.beforeRender.Controller.controller',
- function (Event $event) {
- $view = $event->getSubject();
- if ($view->viewVars['name'] == 'Users') {
- // add the login and logout actions to the Users controller
- $view->set('actions', [
- 'login',
- 'logout',
- 'view',
- 'add',
- 'edit',
- 'delete'
- ]);
- }
- }
- );
By scoping event listeners to specific bake templates, you can simplify yourbake related event logic and provide callbacks that are easier to test.
Bake template files use the Twig template syntax.
So, for example, when baking a shell like so:
The template used (vendor/cakephp/bake/src/Template/Bake/Shell/shell.twig)looks like this:
- <?php
- namespace {{ namespace }}\Shell;
- use Cake\Console\Shell;
- /**
- * {{ name }} shell command.
- */
- class {{ name }}Shell extends Shell
- {
- /**
- * main() method.
- *
- * @return bool|int Success or error code.
- */
- public function main()
- {
- }
- }
And the resultant baked class (src/Shell/FooShell.php) looks like this:
- <?php
- namespace App\Shell;
- use Cake\Console\Shell;
- /**
- * Foo shell command.
- */
- class FooShell extends Shell
- {
- /**
- * main() method.
- *
- * @return bool|int Success or error code.
- */
- public function main()
- {
- }
Note
Prior to version 1.5.0 bake used a custom erb-style tags inside .ctp template files.
<%
A Bake template php open tag%>
A Bake template php close tag- A Bake template php short-echo tag
<%-
A Bake template php open tag, stripping any leading whitespacebefore the tag-%>
A Bake template php close tag, stripping trailing whitespace afterthe tag
If you wish to modify the output produced by the “bake” command, you cancreate your own bake ‘theme’ which allows you to replace some or all of thetemplates that bake uses. The best way to do this is:
Bake a new plugin. The name of the plugin is the bake ‘theme’ name
Copy any templates you want to override fromvendor/cakephp/bake/src/Template/Bake/Template to matching files in yourplugin.
When running bake use the
—theme
option to specify the bake-theme youwant to use. To avoid having to specify this option in each call, you can alsoset your custom theme to be used as default theme:
If you wish to modify the default output produced by the “bake” command, you cancreate your own bake templates in your application. This way does not use the—theme
option in the command line when baking. The best way to do this is:
- Create a new directory /src/Template/Bake/.
- Copy any templates you want to override fromvendor/cakephp/bake/src/Template/Bake/ to matching files in yourapplication.
It’s possible to add new bake command options, or override the ones provided byCakePHP by creating tasks in your application or plugins. By extendingBake\Shell\Task\BakeTask
, bake will find your new task and include it aspart of bake.
As an example, we’ll make a task that creates an arbitrary foo class. First,create the task file src/Shell/Task/FooTask.php. We’ll extend theSimpleBakeTask
for now as our shell task will be simple. SimpleBakeTask
is abstract and requires us to define 3 methods that tell bake what the task iscalled, where the files it generates should go, and what template to use. OurFooTask.php file should look like:
- <?php
- namespace App\Shell\Task;
- use Bake\Shell\Task\SimpleBakeTask;
- class FooTask extends SimpleBakeTask
- {
- public $pathFragment = 'Foo/';
- public function name()
- {
- return 'foo';
- }
- public function fileName($name)
- {
- return $name . 'Foo.php';
- }
- public function template()
- {
- return 'foo';
- }
- }
Once this file has been created, we need to create a template that bake can usewhen generating code. Create src/Template/Bake/foo.twig. In this file we’lladd the following content:
- <?php
- namespace {{ namespace }}\Foo;
- /**
- * {{ name }} foo
- */
- class {{ name }}Foo
- {
- // Add code.
- }
You should now see your new task in the output of . You canrun your new task by running bin/cake bake foo Example
.This will generate a new ExampleFoo
class in src/Foo/ExampleFoo.phpfor your application to use.
- The class suffix will be appened to the name provided in your
bake
call. In the previous example, it would create a file.