Config/bootstrap

cakephp 4.x

In v2.x and 3.x I loaded variables from plugin->config->bootstrap.

I am updating a plugin to 4.x and try to do the same but it does not seem to work the same way.

In src/application :

$this->addPlugin('Medias', ['bootstrap' => true, 'routes' => true]);

In plugin/media/config/bootstrap :

use Cake\Core\Configure;

Configure::write('image_types', ['image/png', 'image/jpeg', 'image/gif']);

And in a view file :


use Cake\Core\Configure;

$image_types = Configure::read('image_types');

debug($image_types) => null

So what’s wrong ?

In your Medias Plugin class, do you have a bootstrap method? My initial guess is that you have to call parent.

class Plugin extends BasePlugin
{
    /**
     * Load all the plugin configuration and bootstrap logic.
     *
     * The host application is provided as an argument. This allows you to load
     * additional plugin dependencies, or attach events.
     *
     * @param \Cake\Core\PluginApplicationInterface $app The host application
     * @return void
     */
    public function bootstrap(PluginApplicationInterface $app): void
    {
        parent::bootstrap($app); // call parent
        
        Configure::write('image_types', ['image/png', 'image/jpeg', 'image/gif']);
    }
....

Since moving/upgrading to CakePHP v4.x I have avoided the bootstrap.php & routes.php files and handled everything via the Plugin class and loaded specific configuration files.

That’s it !

public function bootstrap(PluginApplicationInterface $app): void
    {
        parent::bootstrap($app); // call parent
    }

and my plugin/media/config/bootstrap.php file is left unchanged width various variables.

Thank you.