PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, March 11, 2022

[FIXED] Cannot override BakeHelper using my plugin

 March 11, 2022     cakephp, cakephp-3.0, php     No comments   

Issue

So far, I've been able to override ControllerTask, ModelTask and TemplateTask in my own plugin by adding and aliasing the parent task. Example using ModelTask.

namespace WetKit\Shell\Task;
...
use Bake\Shell\Task\ModelTask as WetKitModelTask;

class ModelTask extends WetKitModelTask
{...}

However, when I attempt to apply the same logic to BakeHelper.php, located in plugin/WetKit/src/View/Helper, it doesn't work and functionality cannot be found:

me@laptop MINGW64 ~/Downloads/xampp3/xampp/htdocs/twofk (master)
$ bin/cake bake controller dummy_lists -t WetKit
*** WetKit Controller Task! ***

Baking controller class for DummyLists...
Warning Error: Method Bake\View\Helper\BakeHelper::isBilingualTable does not exist in [C:\Users\me\Downloads\xampp3\xampp\htdocs\twofk\vendor\cakephp\cakephp\src\View\Helper.php, line 116]

Here is my custom BakeHelper class definition:

namespace WetKit\View\Helper;
...
use Bake\View\Helper as WetKitHelper;

/**
 * Bake helper
 */
class BakeHelper extends WetKitHelper
{
    ...

    public function isBilingualTable($table)
       {...}
}

This will work if I put the file under the vendor folder.


Solution

Helpers cannot be "overwritten" like tasks, as they are loaded completely different. Tasks are being collected in a list, where tasks collected at a later point can replace existing entries in the list in case they are using the same name.

Helpers are loaded via CakePHP's default helper loading functionality, eg loadeHelper(), specifically using a short name that resolves to a fully qualified classname, which cannot be replaced.

Unless you actually want to override any of the bake helper functionality, there's not really a reason to not just use a separate helper, which you can load in the Bake.initialize event:

// in your plugin's bootstrap

\Cake\Event\EventManager::instance()
    ->on('Bake.initialize', function (\Cake\Event\EventInterface $event) {
        /** @var \Cake\View\View $view */
        $view = $event->getSubject();

        // load `\WetKit\View\Helper\CustomBakeHelper`
        $view->loadHelper('WetKit.CustomBake');
    });

The helper can then be used like CustomBake.isBilingualTable(...) in the bake twig templates.

For sake of completion, if you actually needed to overwrite the bake helper, you'd first have to unload the existing one, and then load yours:

$view->helpers()->unload('Bake');
$view->loadHelper('WetKit.Bake');

See also

  • Bake Cookbook > Extending Bake


Answered By - ndm
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing