# 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

setting('message') ?>

``` 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 from `config.php`. ## Backend Admin Pages Register a backend page that appears in the admin panel sidebar: ```php Extensions::registerBackend('my-extension', [ 'title' => 'My Extension', 'include' => __DIR__ . '/admin/index.php', 'icon' => '', ]); ``` 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 [ 'welcome' => 'Welcome!', 'save' => 'Save', ], ]; ``` Use translations in your code: ```php 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 'Sitemap', 'author' => 'Paul Spenke', 'identifier' => 'sitemap', 'licence' => 'MIT', 'version' => '1.0.0', ]; ``` **extension.php:** ```php dirname(__FILE__) . '/routes/sitemap.php' ]); ``` **routes/sitemap.php:** ```php