Add a complete documentation site for Plugi with the following pages: - Readme.md: project overview, quick start, and key features - GettingStarted.md: installation guide, requirements, and first steps - Configuration.md: full reference for .env and config.php settings - Themes.md: theme structure, blocks, layouts, and asset compilation - Extensions.md: extension architecture, routes, blocks, and admin pages Also add index.html (Docsify entry point), _sidebar.md (navigation), favicon.ico, and .nojekyll to prevent GitHub Pages from interfering.
8.4 KiB
Extensions
Extensions (also called plugins) allow you to add functionality to Plugi without modifying the core. They can register routes, blocks, layouts, backend admin pages, assets, settings, and HTML injection points.
How Extensions Work
When Plugi boots, it scans the extensions/ directory. For each subdirectory that contains both an extension.php and a config.php file, it loads the extension and registers its features.
Extension Structure
A minimal extension looks like this:
extensions/
└── my-extension/
├── extension.php # Bootstrap — registers routes, blocks, etc.
├── config.php # Metadata and configuration
├── routes/ # Route handler files (optional)
├── blocks/ # Theme blocks provided by the extension (optional)
├── admin/ # Admin panel files (optional)
├── assets/ # CSS, JS, images (optional)
└── lang/ # Translation files (optional)
The config.php File
Every extension must have a config.php that returns a configuration array:
<?php
return [
'title' => 'My Extension',
'author' => 'Your Name',
'identifier' => 'my-extension',
'licence' => 'MIT',
'version' => '1.0.0',
];
Optional Config Keys
| Key | Description |
|---|---|
settings |
Array of setting definitions for the admin panel (see below). |
assets |
Array of CSS/JS assets to inject on public or admin pages. |
tables |
Database tables the extension requires (see below). |
Extension Settings
Define settings that appear in the admin panel:
'settings' => [
[
'name' => 'email',
'label' => 'Notification Email',
'default' => 'admin@example.com',
'type' => 'text',
],
[
'name' => 'enabled',
'label' => 'Enable Feature',
'default' => 'true',
'type' => 'toggle',
],
],
Supported types: text, toggle, and others depending on the admin UI.
Extension Assets
Register CSS and JS files that should be loaded on specific pages:
'assets' => [
[
'src' => '/assets/dist.css',
'type' => 'style',
'location' => 'header',
],
[
'src' => '/assets/app.js',
'type' => 'script',
'location' => 'footer',
],
],
Valid locations: header, footer, admin-header, admin-footer.
Extension Database Tables
Declare tables that the extension needs. Plugi will create them on first run:
'tables' => [
'entries' => [
'columns' => [
'formId' => 'int NOT NULL',
'data' => 'longtext NOT NULL',
'created' => 'timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP',
],
'uniqueKeys' => [],
],
'definitions' => [
'columns' => [
'name' => 'varchar(255) NOT NULL',
'fields' => 'longtext NOT NULL',
'finishers' => 'longtext NOT NULL',
],
'uniqueKeys' => [
'name' => ['name'],
],
],
],
The extension.php File
This file is executed at boot time. Use the Plugi\Extensions class to register features:
<?php
use Plugi\Extensions;
// Register routes
Extensions::addRoutes([
'/my-route' => dirname(__FILE__) . '/routes/my-route.php',
]);
// Register blocks
Extensions::addBlocks([
'my-block' => __DIR__ . '/blocks/my-block',
]);
// Register layouts
Extensions::addLayouts([
'my-layout' => __DIR__ . '/layouts/my-layout',
]);
// Register a backend admin page
Extensions::registerBackend('my-extension', [
'title' => 'My Extension',
'include' => __DIR__ . '/admin/index.php',
'icon' => '<i class="ph ph-circles-three-plus"></i>',
]);
// Register assets programmatically
Extensions::registerAsset('/extensions/my-extension/app.js', 'script', 'footer');
// Register HTML blocks injected at specific locations
Extensions::registerHtmlBlock('<script>console.log("loaded");</script>', 'head');
Routes
Extensions can define custom routes. Each route maps a URL path to a PHP file:
Extensions::addRoutes([
'/sitemap.xml' => dirname(__FILE__) . '/routes/sitemap.php',
]);
The route file is a regular PHP script that has access to all Plugi globals and classes. Return continue from the route file to let Plugi continue processing the request:
<?php
// routes/example.php
echo "Hello from extension!";
return 'continue'; // Let Plugi continue with normal page rendering
If the route file does not return continue, Plugi stops processing and the route handles the entire response.
Blocks
Extensions can provide blocks that appear in the page builder's block library. A block directory contains the same files as a theme block:
blocks/
└── my-block/
├── config.php # Block configuration (title, icon, settings)
├── view.html # HTML template (or view.php for PHP blocks)
└── script.js # Optional JavaScript
Block config.php example:
<?php
return [
'title' => 'My Block',
'category' => 'Custom',
'icon' => 'fa fa-star',
'settings' => [
'message' => [
'label' => 'Message',
'type' => 'text',
'value' => 'Hello World',
],
],
];
In view.html, access settings with the $block variable:
<div class="my-block">
<p><?= $block->setting('message') ?></p>
</div>
For PHP blocks, use view.php instead. The $block object provides:
$block->setting('key')— Get a block setting value.$block->get('config_key')— Get a value fromconfig.php.
Backend Admin Pages
Register a backend page that appears in the admin panel sidebar:
Extensions::registerBackend('my-extension', [
'title' => 'My Extension',
'include' => __DIR__ . '/admin/index.php',
'icon' => '<i class="ph ph-circles-three-plus"></i>',
]);
The include file is a PHP view rendered inside the admin panel. It has access to all Plugi classes and helpers.
Translations
Place translation files in the lang/ directory:
lang/
├── en.php
└── de.php
Each file returns an associative array:
<?php
// lang/en.php
return [
'my-extension' => [
'welcome' => 'Welcome!',
'save' => 'Save',
],
];
Use translations in your code:
phpb_trans('my-extension.welcome')
Helper Functions
The following global helper functions are available in extension code:
| Function | Description |
|---|---|
phpb_config('key') |
Get a config value using dot notation. |
phpb_trans('key') |
Get a translated string. |
phpb_e($string) |
Escape output for HTML. |
phpb_current_language() |
Get the current language code. |
phpb_current_relative_url() |
Get the current request URL. |
phpb_redirect($url) |
Redirect to a URL. |
phpb_instance($key, $args=[]) |
Create an instance of a configured class. |
phpb_asset($path) |
Generate a URL to a Plugi core asset. |
phpb_theme_asset($path) |
Generate a URL to a theme asset. |
Example: Sitemap Extension
The plugi-ext-sitemap extension demonstrates a minimal extension:
config.php:
<?php
return [
'title' => 'Sitemap',
'author' => 'Paul Spenke',
'identifier' => 'sitemap',
'licence' => 'MIT',
'version' => '1.0.0',
];
extension.php:
<?php
use Plugi\Extensions;
Extensions::addRoutes([
'/sitemap.xml' => dirname(__FILE__) . '/routes/sitemap.php'
]);
routes/sitemap.php:
<?php
// Generate and output XML sitemap
header('Content-Type: application/xml');
// ... sitemap generation logic
Example: Form Extension
The plugi-ext-form extension demonstrates a full-featured extension with routes, blocks, admin pages, settings, and database tables. See the extensions/plugi-ext-form/ directory for the complete source.