dvlpr
dvlpr

Reputation: 122

Composer autoloader

I'm trying to learn composer, but I can't seem to get the autoloader to work with a package I've created. It just states that the class could not be found.

What am I doing wrong?

Index.php:

require 'vendor/composer/autoload_psr4.php';
use packageName\core\Bootstrap;
$boot = new Bootstrap();

Bootstrap.class.php (inside /vendor/vendorName/packageName/core/):

namespace packageName\core;
class Bootstrap {
   ...

composer.json for packageName:

"autoload": {
    "psr-4": { "packageName\\core\\": "/vendor/vendorName/packageName/core" }
}

Upvotes: 0

Views: 302

Answers (2)

dvlpr
dvlpr

Reputation: 122

Mark Baker's answer (in the comments above) fixed the problem.

"Is /vendor/... really at root level in your filesystem? If it's a relative path rather than an absolute path, use "vendor/vendorName/packageName/core"

Upvotes: 0

Sven
Sven

Reputation: 70853

First: Don't include a random autoload component. Composer has documentation how to use the autoloader.

Second: Don't deal with packages that are already installed with Composer in your OWN autoloading. Everything that is inside the vendor folder must not be autoloaded from within your own composer.json - it should supply it's own autoloading definition. The easiest case would be you having only this:

{
    "require": {
        "vendorName/packageName" : "^1.0"
    }
}

You would only need to add autoloading to this if you want to have your own code autoloaded as well (which I would recommend).

Upvotes: 1

Related Questions