Extend NativePHP Mobile with plugins that add native functionality
The NativePHP Mobile plugin system allows you to extend your app with native functionality through reusable packages. Plugins can add bridge functions, permissions, native dependencies, and platform-specific code.
Plugins are discovered automatically from your Composer dependencies by scanning for packages with type nativephp-plugin. For security, plugins must be explicitly registered in your NativeServiceProvider to be loaded.
The plugin registry scans vendor/composer/installed.json for packages with:
Type: nativephp-plugin
A nativephp.json manifest file
Registered in your NativeServiceProvider
use Native\Mobile\Plugins\PluginRegistry;$registry = PluginRegistry::getInstance();// Get all registered plugins$plugins = $registry->all();// Get all installed plugins (including unregistered)$allInstalled = $registry->allInstalled();// Get plugins that are installed but not registered$unregistered = $registry->unregistered();
Plugins must be explicitly allowed in your NativeServiceProvider to prevent transitive dependencies from auto-registering potentially malicious code:
namespace App\Providers;use Illuminate\Support\ServiceProvider;class NativeServiceProvider extends ServiceProvider{ /** * Return an array of allowed plugin service provider classes. */ public function plugins(): array { return [ \NativePHP\Haptics\HapticsServiceProvider::class, \NativePHP\Biometrics\BiometricsServiceProvider::class, // Add more plugins here ]; }}
If the NativeServiceProvider doesn’t exist or the plugins() method is missing, all plugins are blocked by default for security.
The PluginRegistry provides methods for querying plugins:
use Native\Mobile\Plugins\PluginRegistry;$registry = PluginRegistry::getInstance();// Check if a plugin is registered$registry->isRegistered('nativephp/plugin-haptics');// Find a specific plugin$plugin = $registry->find('nativephp/plugin-haptics');// Check if plugin exists$registry->has('nativephp/plugin-haptics');// Count registered plugins$count = $registry->count();// Get all bridge functions$functions = $registry->bridgeFunctions();// Get all Android permissions$permissions = $registry->androidPermissions();// Get all iOS Info.plist entries$infoPlist = $registry->iosInfoPlist();// Get plugins with Android native code$androidPlugins = $registry->withAndroidCode();// Get plugins with iOS native code$iosPlugins = $registry->withIosCode();// Get plugins with events$eventPlugins = $registry->withEvents();