I 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?

有帮助吗?

解决方案

Without 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';
}
许可以下: CC-BY-SA归因
scroll top