PluginContract Interface
All plugins must implement the PluginContract interface, which defines the plugin lifecycle.
<?php
namespace Fundorex\Plugin;
interface PluginContract
{
public function getName(): string;
public function getVersion(): string;
public function install(): void;
public function uninstall(): void;
public function enable(): void;
public function disable(): void;
public function isEnabled(): bool;
}
Lifecycle Methods
getName()
Returns the unique plugin identifier.
public function getName(): string
{
return 'my-plugin';
}
Usage: Plugin identification and reference in hooks and events.
getVersion()
Returns the plugin version (semantic versioning).
public function getVersion(): string
{
return '1.0.0';
}
Usage: Version tracking and compatibility checks.
install()
Runs when the plugin is installed. Use this to:
- Create database tables
- Publish plugin assets
- Initialize default configuration
- Create required directories
public function install(): void
{
// Run migrations
Artisan::call('migrate', [
'--path' => __DIR__ . '/../../database/migrations',
]);
// Publish assets
Artisan::call('vendor:publish', [
'--tag' => 'my-plugin-assets',
]);
// Log installation
Log::info('Plugin installed: ' . $this->getName());
}
Timing: Called once when the admin clicks "Install" in the plugin manager.
uninstall()
Runs when the plugin is uninstalled. Use this to:
- Drop database tables
- Remove plugin data
- Clean up uploaded files
- Remove configurations
public function uninstall(): void
{
// Rollback migrations
Artisan::call('migrate:rollback', [
'--path' => __DIR__ . '/../../database/migrations',
]);
// Delete plugin data
DB::table('plugin_data')->where('plugin', $this->getName())->delete();
// Log uninstallation
Log::info('Plugin uninstalled: ' . $this->getName());
}
Warning: This is destructive. Ensure data backup before uninstalling.
Timing: Called when the admin clicks "Uninstall" and confirms removal.
enable()
Runs when the plugin is enabled. Use this to:
- Register routes
- Load service providers
- Enable scheduled tasks
- Start event listeners
public function enable(): void
{
// Mark plugin as enabled in database
DB::table('plugins')
->where('name', $this->getName())
->update(['enabled' => true]);
// Log enabling
Log::info('Plugin enabled: ' . $this->getName());
}
Timing: Called when the admin clicks "Enable".
disable()
Runs when the plugin is disabled. Use this to:
- Unregister routes
- Stop event listeners
- Pause scheduled tasks
- Flush caches
public function disable(): void
{
// Mark plugin as disabled in database
DB::table('plugins')
->where('name', $this->getName())
->update(['enabled' => false]);
// Flush related caches
Cache::forget('plugin:' . $this->getName());
// Log disabling
Log::info('Plugin disabled: ' . $this->getName());
}
Timing: Called when the admin clicks "Disable".
isEnabled()
Returns whether the plugin is currently enabled.
public function isEnabled(): bool
{
return DB::table('plugins')
->where('name', $this->getName())
->value('enabled') ?? false;
}
Usage: Conditional feature loading and hook registration.
Lifecycle Flow
Installation Phase
├── install() called
├── Database migrations run
├── Assets published
└── Plugin marked as installed
Enable/Disable Cycle
├── User clicks Enable → enable() called
├── Routes and listeners active
├── User clicks Disable → disable() called
└── Routes and listeners inactive
Uninstallation Phase
└── User confirms uninstall → uninstall() called
├── Migrations rolled back
├── Plugin data deleted
└── Plugin removed from system
Best Practices
Error Handling
Always wrap lifecycle methods with error handling:
public function install(): void
{
try {
Artisan::call('migrate', [...]);
} catch (Exception $e) {
Log::error('Plugin installation failed: ' . $e->getMessage());
throw $e;
}
}
Idempotency
Make methods safe to call multiple times:
public function enable(): void
{
// Check if already enabled
if ($this->isEnabled()) {
return;
}
// Enable logic...
}
Data Integrity
Always back up data before uninstalling:
public function uninstall(): void
{
// Create backup
Artisan::call('plugin:backup', [
'plugin' => $this->getName(),
]);
// Then delete
DB::table('plugin_data')->where('plugin', $this->getName())->delete();
}
Logging
Log all lifecycle events for debugging:
public function install(): void
{
Log::info('Installing plugin: ' . $this->getName());
// ... installation logic ...
Log::info('Plugin installed successfully: ' . $this->getName());
}
Next Steps
- Hooks Reference — Available plugin hooks
- Menus, Assets & Routes — UI integration
- Permissions, Settings & Data — Plugin data management

