cakephpcakephp-3.xcakephp-bake

Cakephp 3.x bake - get cli options inside templates


In Cakephp 3.x when baking e.g. templates for admin I run.

 bin/cake bake template Posts --prefix admin

This works fine and files are generated successfully, however how inside bake' templates can I check if that prefix option is set or not. I need to check that condition if this is for admin to make different output.

Thanks


Solution

  • As far as I can tell the value is currently not available out of the box, unlike the controller task, the template task doesn't pass the prefix to the view, see

    This is something for a feature request that you could issue over at GitHub, or you could even push a PR yourself.

    If you can't wait for this to be implemented, you could create your own extended bake command, ie extend the template task, overwrite TemplateTask::_loadController(), and add the prefix (TemplateTask::_getPrefix()) to the returned array, something along the lines of

    <?php
    namespace App\Shell\Task;
    
    use Bake\Shell\Task\TemplateTask;
    
    class MyTemplateTask extends TemplateTask
    {
        protected function _loadController()
        {
            return parent::_loadController() + [
                'prefix' => $this->_getPrefix()
            ];
        }
    }
    

    The value should then be available as $prefix in your bake template.

    See also