# 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 '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: ```php '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: ```php '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: ```php '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 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' => '', ]); // Register assets programmatically Extensions::registerAsset('/extensions/my-extension/app.js', 'script', 'footer'); // Register HTML blocks injected at specific locations Extensions::registerHtmlBlock('', 'head'); ``` ## Routes Extensions can define custom routes. Each route maps a URL path to a PHP file: ```php 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 '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: ```html
= $block->setting('message') ?>