是否可以自定义bake(CakePHP 3)添加的动作?

时间:2018-07-06 18:09:10

标签: cakephp-3.x cakephp-bake

我希望能够不使用“ view”操作来烘焙控制器和视图。 是否有任何参数或方法可以跳过甚至自定义烘焙命令的动作?

1 个答案:

答案 0 :(得分:1)

我发现我可以使用事件来修改烘焙的行为。

将以下代码添加到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;
        }
    }
);

更多信息,请访问:https://book.cakephp.org/3.0/en/bake/development.html

还要检查:https://github.com/cakephp/bake/issues/164