Skip to content

Plugins

Arthu Santiago edited this page Nov 30, 2026 · 1 revision

Plugins

A local plugin is how an installation adds to KeyAnyWhere without editing the core: a menu link, a notice on every page, a different insecure password check, an API. Because the core files stay untouched, upgrades remain a plain git checkout. This tutorial creates a first plugin that adds a link to the menu, and then explains how plugins are loaded and configured.

Before you start

A development environment to try the plugin first. The same files then go to production.

1. Create the plugin class

A plugin lives in plugins/<Name>/, which Git ignores in this repository. Its class tells CakePHP that the folder is a plugin. This step creates plugins/MyPlugin with its class.

mkdir -p plugins/MyPlugin/src plugins/MyPlugin/config
cat > plugins/MyPlugin/src/MyPluginPlugin.php <<'EOF'
<?php
declare(strict_types=1);

namespace MyPlugin;

use Cake\Core\BasePlugin;

/**
 * Local plugin of this installation.
 */
class MyPluginPlugin extends BasePlugin
{
}
EOF

2. Load the plugin

config/plugins_local.php (ignored by Git) lists the plugins of the installation. It also registers each plugin's namespace in Composer's autoloader, so composer.json stays untouched: editing it would conflict with the next upgrade.

cat > config/plugins_local.php <<'EOF'
<?php
// Plugins of this installation (ignored by Git). Each plugin's namespace is registered in
// Composer's autoloader here, so composer.json stays untouched and upgrades merge cleanly.
$loader = require dirname(__DIR__) . '/vendor/autoload.php';
$loader->addPsr4('MyPlugin\\', dirname(__DIR__) . '/plugins/MyPlugin/src/');

return [
    'MyPlugin' => [],
];
EOF

Check that CakePHP sees it:

bin/cake plugin loaded

MyPlugin is in the list.

3. Add a menu item

A plugin's config/bootstrap.php runs on every request. This one adds a link to the wiki in the Outros (Other) section of the menu. It writes the key only when it isn't set yet, so the installation's config/app_local.php can still override it (Extension Points explains why).

cat > plugins/MyPlugin/config/bootstrap.php <<'EOF'
<?php
declare(strict_types=1);

use Cake\Core\Configure;

if (!Configure::check('Kaw.Menu.outros')) {
    Configure::write('Kaw.Menu.outros', [
        [
            'url' => 'https://github.com/arthusantiago/KeyAnyWhere/wiki',
            'icone' => 'bi-book',
            'rotulo' => 'Documentação',
        ],
    ]);
}
EOF

4. Check that it worked

Start the server (bin/cake server), log in and open the menu: a Documentação item with a book icon appears under Outros.

5. Take it to production

Copy plugins/MyPlugin/ and config/plugins_local.php to the same paths in /var/www/keyanywhere, run the steps of File Permissions and restart Apache. Add both to your backups.

How plugins are configured

  • Loading order: the core's own plugins (Migrations, DebugKit in development) are loaded by Application::bootstrap(); config/plugins.php adds the ones in config/plugins_local.php, in the order they are listed.
  • Plugin configuration file: plugins/<Name>/config/app.php is merged after the core's config/app.php and before config/app_local.php, so it can change what the core's bootstrap uses (Cache, Datasources, Log, Error, EmailTransport) before any plugin's bootstrap() runs. Declare only the keys you change; numbered lists are appended to.
  • Plugin hooks: override bootstrap(), routes(), middleware(), services() or events() in the plugin class, as described in the CakePHP plugin chapter.
  • Dependency rule: a plugin may use anything in the core; the core never uses a plugin.

Next step

Extension Points

Clone this wiki locally