Repository navigation
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.
A development environment to try the plugin first. The same files then go to production.
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/configcat > 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
{
}
EOFconfig/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' => [],
];
EOFCheck that CakePHP sees it:
bin/cake plugin loadedMyPlugin is in the list.
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',
],
]);
}
EOFStart the server (bin/cake server), log in and open the menu: a Documentação item with a book icon appears under Outros.
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.
-
Loading order: the core's own plugins (Migrations, DebugKit in development) are loaded by
Application::bootstrap();config/plugins.phpadds the ones inconfig/plugins_local.php, in the order they are listed. -
Plugin configuration file:
plugins/<Name>/config/app.phpis merged after the core'sconfig/app.phpand beforeconfig/app_local.php, so it can change what the core's bootstrap uses (Cache,Datasources,Log,Error,EmailTransport) before any plugin'sbootstrap()runs. Declare only the keys you change; numbered lists are appended to. -
Plugin hooks: override
bootstrap(),routes(),middleware(),services()orevents()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.
Source code · Issues · Releases · Packagist · Online demo · Hosted edition
1. Getting started
2. Installing in production
- Overview
- Preparing the Server
- Installing KeyAnyWhere
- File Permissions
- Generating Security Keys
- Environment Variables
- Configuration Files
- Database Setup
- Insecure Password Catalog
- Web Server and HTTPS
- First-Run Setup
3. Operating your installation
4. Using KeyAnyWhere
5. Security
6. Development
7. Extending