CakePHP 4 Plugin use third libraries - Class not found

I’m currently writing a cakephp 4 plugin. I use a few libraries via composer install.
Problem when running the main cakephp application my app cannot find the library classes.
Is there another way to use other libraries for a plugin in Cakephp 4?

if your plugin is installed via composer require myname/mypackage it should install all your dependencies according to your plugins composer.json as usual.

So they should be installed in your main apps vendor folder

(DE) Hallo Kevin, vielen Dank für deine Antwort. Ich glaube, ich konnte mein Problem nicht genau ausdrücken. Ich versuche, es noch deutlicher zu beschreiben.

Ich habe unter /plugins ein eigenes Plugin für das Schreiben von Blogbeiträgen erstellt. Eine Hilfskomponente unter /plugins/BlogEntrys/src/component/BlogComponent.php benötigt für das Plugin eine externe Bibliothek, die ich im /plugins/BlogEntrys-Verzeichnis per Composer installiert habe. Innerhalb des Plugins funktioniert auch der Aufruf der Klasse der Bibliothek, jedoch nur, weil ich die /plugins/BlogEntrys/vendor/autoload.php in die Komponente als require() hinzugefügt habe. Allerdings finde ich diese Lösung nicht besonders elegant und wollte nachfragen, ob es vielleicht eine angenehmere Lösung gibt.

(EN)
Hello Kevin, thank you very much for your response. I believe I couldn’t express my problem accurately. I’ll try to describe it more clearly.

I’ve created a custom plugin for writing blog posts under /plugins. A helper component located at /plugins/BlogEntrys/src/component/BlogComponent.php requires an external library for the plugin, which I installed in the /plugins/BlogEntrys directory using Composer. Within the plugin, the invocation of the library class works because I added /plugins/BlogEntrys/vendor/autoload.php to the component using require(). However, I find this approach inelegant and wanted to inquire if there might be a more graceful solution.

:v:I also wrote it in German. Just so I can express myself better :slight_smile:

I was expecting that this will probably come up, so let me quickly try to explain whats happening:

Private CakePHP plugins (those located inside your plugins folder) are NOT managed by composer. Therefore, composer doesn’t understand/care about what you write in your plugins/MyPlugin/composer.json and therefore doesn’t install its dependencies in your root vendor folder (which is your main vendor folder and the place of the autoloader which is loaded by the webroot/index.php as well as bin/cake.php)

Composer only cares about your root composer.json and will install its dependencies inside your root vendor folder.


Before CakePHP 5 you had to manually map your private plugins namespaces in your root composer.json as is described in Plugins - 4.x

With CakePHP 5 (and more directly speaking the plugin-installer v2) ONLY the autoload configs of your plugins will be automatically imported, but NOT the require or require-dev sections. This means only the Namespace definitions will be imported automatically.


It is easiest for you right now if you just have all your dependencies in your root composer.json and not distributed across your private plugins.

Unless you want to write your own composer plugin/class which does that for you (similar to what we already do with our https://github.com/cakephp/plugin-installer/blob/2.x/src/Plugin.php)

Thanks, Kevin, for your very detailed response. Now I understand it. I thought that the plugins are standalone components, which is why I used composer install in the plugins. I will rewrite the composer.json in the root, and that should fix the issue. Also, thanks for the hint about CakePHP 5. The migration is also on my agenda shortly. :slight_smile: