Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this questionI want to use composer to load my custom classes inside my plugin. I've tried to use it but without success. Is possible to use compose for the plugin develop to manage custom classes used inside it? Can anyone point me into the roght direction?
Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this questionI want to use composer to load my custom classes inside my plugin. I've tried to use it but without success. Is possible to use compose for the plugin develop to manage custom classes used inside it? Can anyone point me into the roght direction?
Share Improve this question asked Oct 7, 2019 at 17:10 userone2userone2 441 silver badge5 bronze badges 4 |1 Answer
Reset to default 1Without more context from you, I can only assume and show you what I have done that works for me using PSR4 Autoloading.
Example:
Assuming that all my custom class directories and files is in ./inc folder
In your composer.json, add this
"autoload": {
"psr-4": {
"Inc\\": "./inc"
}
}
Inc is the vendor name of your application, use this for namspacing files inside your inc directory like namespace Inc/Api;
./inc is the directory(with all the class files or nested directory) you want to autoload.
Next, do this in your terminal to generate the vendor directory & autoload the files.
composer dump-autoload
Lastly, require the the autoloading by adding this to your plugin file eg. my-awesome-plugin.php
if (file_exists(dirname(__FILE__) . '/vendor/autoload.php')) {
require_once dirname(__FILE__) . '/vendor/autoload.php';
}
require_once
, this is not the best option because the plugin will become more modular in the future. @TomJNowell Yes, the autoloader will be used only internally from the plugin – userone2 Commented Oct 7, 2019 at 17:38