If your plugin depends on a library you have required via Composer, that library needs to be available with the ZIP download and Git submodule methods as well. Let's see how.
There is a Pluginkit example plugin for this tutorial as well. You can find its code in the
3-composer branch of the Pluginkit.
If you want to follow along, you can download a ZIP file of that branch or get it via Composer:
composer create-project getkirby/pluginkit site/plugins/your-plugin dev-3-composer --remove-vcs
Let's say your plugin requires a (fictional) library that is published as the Composer package
superwoman/superlibrary. To add it as a dependency, run the following commands:
cd site/plugins/your-plugin composer require superwoman/superlibrary
Composer will now install the library to your
vendor directory and add the library to your
composer.json file for you:
By adding the dependency to the
composer.json file, the library will be automatically installed together with your plugin if the user of your plugin uses Composer to do so.
The issue is that the library's code won't be available if the plugin is installed via a Git submodule or manually. For that, you need to include the code in your Git repository by modifying the rules in the
We have prepared a set of rules that still ignores most files that are not necessary, but includes all relevant files of your dependencies:
The next step is to load the Composer autoloader from your plugin's
index.php file. This will automatically load the required libraries when needed:
Note that you should not
require the autoloader, as it will only be present if the plugin is installed manually or via a Git submodule. Our custom Composer installer will delete the
vendor directories from plugins to avoid code duplication and autoloading issues. By using
@include_once, you tell PHP to load the file only if it exists.
Now that you are using the Composer autoloader anyway, you might as well use it for your plugin's own classes as well instead of using Kirby's autoloader like in the plugin setup with autoloader.
First step is to add the classes and files to load to your plugin's
PSR-4, Composer provides several other options for autoloading.
When you are done configuring, you need to run
composer install again to regenerate Composer's autoloader. Afterwards commit the changes Composer made to the
vendor directory to your Git repo.