0

I'm making a CakePHP3 plugin and I'm using a library that I wrote that uses PSR-4 autoloading. If I require the library in the application's composer.json it autoloads correctly and I can use it anywhere in my app including the plugin. When I try requiring it through my plugin's composer.json it installs fine and composer gives no errors but it doesn't seem to autoload. When I go a page controlled by the plugin that uses the library I get the error that the class is not found.

Am I doing something wrong? I checked vendor/composer/autoload_psr4.php in my plugin folder and my class is in that file. I also ran composer dump-autoload in both the application directory and the plugin directory but it didn't help.

I baked the plugin with cake bake plugin MyPlugin and then edited the require section of the plugin's composer.json to include my library. Then I ran composer update and it installed the library to plugins/MyPlugin/vendor/mylibrary without errors.

Devin Crossman
  • 7,454
  • 11
  • 64
  • 102
  • How are you installing the plugin? Did you put your plugin in packagist for example? – José Lorenzo Rodríguez Jun 28 '15 at 21:36
  • the plugin is not in packagist but the library I'm trying to require in the plugin is in packagist – Devin Crossman Jun 28 '15 at 21:38
  • I don;t have time right now for a full answer, but your best bet is puttnig your plugin in packagist or using the vcs composer install. Otherwise composer will have no idea where to find your code and how to calculate its dependencies – José Lorenzo Rodríguez Jun 28 '15 at 21:41
  • @JoséLorenzo I moved my baked plugin into its own git repo and included it as a vcs repo in my app's composer.json. It installed and my library is autoloaded, I can use it in the app, but the plugin is installed into the vendor directory and not the plugins directory so my plugin does not work. Do you know how to fix that? – Devin Crossman Jun 28 '15 at 22:20
  • @JoséLorenzo nvm it looks like its working okay now with the plugin in the vendor directory. It is referenced in `vendor/cakephp-plugins.php` all I needed to do was load the plugin in `config/bootstrap.php` – Devin Crossman Jun 28 '15 at 22:34

2 Answers2

0

I moved my baked plugin into its own git repo and then added that repo to my app's composer.json file and added my plugin as a requirement. Then I ran composer update in my app directory and my plugin was installed with it's dependencies into the vendor directory. Then I just needed to load the plugin in config/bootstrap.php and everything worked!

Devin Crossman
  • 7,454
  • 11
  • 64
  • 102
0

There seems to be something wrong while generating the file vendor/cakephp-plugins.php. Somehow the CakePHP plugins you install yourself are not included in this file. When adding the plugins yourself, everything works fine, but everytime you load composer update/install, this file is re-generated and so you'll lose your changes.

I can see the plugins are loaded in vendor/composer/installed.json, but the problem seems to be the fact that their type is 'library', where it should be cakephp-plugin. I can't figure out what needs to be changed so CakePHP sees this as an own plugin.