Search code examples
cakephp-3.xcakephp-bake

Is it possibile customize the actions added by bake (CakePHP 3)?


I want to be able to bake controllers and views without using the "view" action. Is there any parameter or way to skip or even customize the actions for bake commands?


Solution

  • I found that I can use events to modify bake's behaviour.

    Add the following code to bootstrap_cli.php

    use Cake\Event\Event;
    use Cake\Event\EventManager;
    use Cake\Utility\Hash;
    // put all 'use' statements at the top of the file
    
    EventManager::instance()->on(
        'Bake.beforeRender.Controller.controller',
        function (Event $event) {
            $view = $event->getSubject();
            if(!empty($view->viewVars['actions'])) {
                $messages = $view->viewVars['actions'];
                $del_val = 'view';
                if (($key = array_search($del_val, $messages)) !== false) {
                    unset($messages[$key]);
                    echo "\n";
                    echo '====== NOTICE ======' . "\n";
                    echo 'Removed view action.' . "\n";
                    echo '====================' . "\n";
                }
                $view->viewVars['actions'] = $messages;
            }
        }
    );
    

    More info at: https://book.cakephp.org/3.0/en/bake/development.html

    Also check: https://github.com/cakephp/bake/issues/164