TemplatePlugin
extends BasePlugin
in package
implements
KMPPluginInterface
Template Plugin - Complete Boilerplate for KMP Plugin Development
This plugin serves as a comprehensive template and example for creating new plugins within the Kingdom Management Portal (KMP) system. It demonstrates all common plugin components, patterns, and integration points.
Purpose
The Template plugin provides:
- Reference Implementation: Working examples of all plugin components
- Quick Start: Copy and modify to create new plugins
- Best Practices: Demonstrates KMP coding standards and patterns
- Complete Integration: Shows navigation, authorization, and UI integration
Features Demonstrated
Core Components
- Controllers: HelloWorldController with standard CRUD patterns
- Models: Example Table and Entity classes with proper configuration
- Policies: HelloWorldPolicy for RBAC authorization
- Services: Business logic separation and dependency injection
KMP Integration
- Navigation: Automatic menu registration with NavigationRegistry
- View Cells: Dashboard widget integration examples
- Authorization: Policy-based access control with KMP's RBAC system
- Routing: Plugin-specific route configuration
Frontend Components
- Stimulus.js Controllers: Interactive frontend behavior
- Templates: Bootstrap-styled view templates
- CSS Styling: Plugin-specific stylesheet examples
Database
- Migrations: Example table creation and schema management
- Seeds: Sample data for development and testing
Architecture
This plugin follows KMP's service-oriented architecture:
- Controllers handle HTTP requests and coordinate responses
- Services contain business logic and workflow management
- Policies enforce authorization rules
- Models manage data access and persistence
- View cells provide reusable dashboard components
Usage as Template
To create a new plugin from this template:
- Copy the entire
plugins/Templatedirectory - Rename to your plugin name (e.g.,
plugins/MyPlugin) - Search and replace "Template" with "MyPlugin" throughout
- Update composer.json with your plugin details
- Register in config/plugins.php with appropriate migration order
- Customize the components for your specific needs
Tags
Table of Contents
Interfaces
- KMPPluginInterface
- Plugin architecture contract for KMP plugins.
Properties
- $_migrationOrder : int
- Plugin migration order for KMP plugin system
Methods
- __construct() : mixed
- Constructor
- bootstrap() : void
- Plugin Bootstrap Process
- console() : CommandCollection
- Configure Plugin Console Commands
- getMigrationOrder() : int
- Get Migration Order
- middleware() : MiddlewareQueue
- Configure Plugin Middleware
- routes() : void
- Configure Plugin Routes
- services() : void
- Configure Plugin Services
- initializeSettings() : void
- Initialize Plugin Settings
Properties
$_migrationOrder
Plugin migration order for KMP plugin system
protected
int
$_migrationOrder
= 0
Migration order priority for database setup
Methods
__construct()
Constructor
public
__construct([array<string|int, mixed> $config = [] ]) : mixed
Initialize the plugin with migration configuration from plugins.php.
Parameters
- $config : array<string|int, mixed> = []
-
Plugin configuration including migrationOrder
bootstrap()
Plugin Bootstrap Process
public
bootstrap(PluginApplicationInterface $app) : void
This method is called during application bootstrap and handles:
- Navigation item registration
- View cell registration for dashboard widgets
- Event listener attachment
- Service initialization
The bootstrap process runs early in the application lifecycle, making it ideal for registering services and setting up plugin infrastructure.
Parameters
- $app : PluginApplicationInterface
-
The application instance
console()
Configure Plugin Console Commands
public
console(CommandCollection $commands) : CommandCollection
Register CLI commands that are specific to this plugin. Commands can be used for:
- Data migration and import
- Batch processing
- Maintenance tasks
- Report generation
Parameters
- $commands : CommandCollection
-
The command collection to modify
Return values
CommandCollection —The modified command collection
getMigrationOrder()
Get Migration Order
public
getMigrationOrder() : int
Returns the migration order priority for this plugin, which determines the sequence in which plugin migrations are executed during system setup. The Template plugin uses default order (0) as it has no special dependencies.
Return values
int —Migration order priority
middleware()
Configure Plugin Middleware
public
middleware(MiddlewareQueue $middlewareQueue) : MiddlewareQueue
Add middleware specific to this plugin. Middleware can handle:
- Request preprocessing
- Response postprocessing
- Authentication checks
- Rate limiting
- Custom header management
Parameters
- $middlewareQueue : MiddlewareQueue
-
The middleware queue to modify
Return values
MiddlewareQueue —The modified middleware queue
routes()
Configure Plugin Routes
public
routes(RouteBuilder $routes) : void
Defines the URL routes for this plugin. All plugin routes are automatically prefixed with the plugin path (e.g., /template/...).
The plugin uses fallback routing for standard controller/action patterns, but custom routes can be added for specific workflows.
Parameters
- $routes : RouteBuilder
-
The route builder to configure
services()
Configure Plugin Services
public
services(ContainerInterface $container) : void
Register services in the dependency injection container. This is where you bind interfaces to implementations for service classes.
Services registered here can be injected into controllers, other services, and commands through constructor injection.
Parameters
- $container : ContainerInterface
-
The DI container
initializeSettings()
Initialize Plugin Settings
protected
initializeSettings() : void
Sets up default configuration values in the KMP settings system. This ensures that the plugin has sensible defaults on first installation.
Settings are versioned to allow automatic updates when the plugin is upgraded.