Extending a Core Helper

I’ve got a plugin with a FormHelper that I’m wanting to extend the core FormHelper with. I can’t seem to figure out how to do it. I’m using CakePHP4.

Here’s what I have so far (excluding other things I’ve tried):

<?php
declare(strict_types=1);

namespace CakeBootstrap\View\Helper;

use Cake\View\Helper\FormHelper as Helper;
use Cake\View\View;

/**
 * Form helper
 */
class FormHelper extends Helper
{
    /**
     * Default configuration.
     *
     * @var array
     */
    protected $_defaultConfig = [];

    public function __construct(View $View, array $config = [])
    {
        parent::__construct($View, $config);
    }
    
}

Then in my AppView I’ve tried loading it in a couple of different ways, but they all throw a MissingHelperException.

$this->loadHelper('CakeBootstrap.Form');

and the following, which I don’t even know if its something that can be done:

$this->loadHelper('Form', ['className' => 'CakeBootstrap.Form']);

Any help would be appreciated. Thanks.

Does anyone have any thoughts on this. Still haven’t figured it out.

Out of frustration and went in and baked a brand new plugin. Then baked a new helper for that plugin. Tried to load that plugin in the same exact way in the AppView and it says it can’t find it.

If you do use CakeBootstrap\View\Helper; and then $x = new FormHelper(null); do you get an error about the class not existing, or something else? If it says the class can’t be found, then it sounds like maybe an autoloader issue.

Did you load the plugin in your Application bootstrap?

Yes, I loaded the plugin in the Application.php file.

Yea, I’ve wondered about the autoloading issue. I’ve even started looking at the ObjectRegistry since that’s what’s throwing the exception, but surely this is not a framework issue. I’m even having issue debugging that. Tomorrow I was going to do a fresh install and try again. If I get the same results, I’m not really sure what else to assume.

I guess class_exists('CakeBootstrap\View\Helper\FormHelper') would be an easier way to check if the autoloader is working.

After you bake a plugin you should do (afaik bakes calls it)

composer dump-autoload

What is the composer command supposed to do? I ran it and it output:

Cake\Composer\Installer\PluginInstaller::postAutoloadDump

I don’t really know what to make of that.

The composer dump-autoload regenerate composer classlist, It make sure you can use your new baked plugin

Ok, so I’m still getting some errors, but I think I can work through these. I’ve made progress at least.

So what happened?

I had no idea that the plugins needed to load via composer and put in the composer file. These are plugins that we’re managing in our own repository and I didn’t realize that composer was a requirement to mange them. I noticed in the plugin documentation that information is there. I hadn’t read that until now. We’ve been working with CakePHP2 for years and version 2 didn’t work that way, so I had no idea that version 4 would work that way. Anyway, now I know. Wanted to leave this information in case anyone else runs into the same problem.

Thanks for the guidance.

Hi @justinkbug,

You surely do like this:

$this->loadHelper('Form', ['className' => 'CakeBootstrap.Form']);

Where CakeBootstrap is plugin name and Form is FormHelper.php class name.

But as @raul338 suggested you have to regerate your composer class list in order to use it in your application. Just run same command but add -o flag.

composer dump-autoload -o