İbrahim Duran
İbrahim Duran

Reputation: 378

Composer doesn't autoload packages

I'm currently developing a framework but I couldn't figure out how am I going to set autoloading. First I created a package with sample class and composer.json. I've autoloaded that sample class by:

"autoload": {
    "classmap": [
      "libs/"
    ]
}

I've checked /vendor/mypackage/vendor/composer/autoload_classmap.php and confirmed that package's autoloader is working fine. But the problem is I can't reach that package's class from main app unless I directly include that package's autoload.php.

UPDATE

/vendor/foo/mypackage/composer.json

"autoload": {
  "psr-4": {
     "Http\\": "libs/"
   }
}

/vendor/foo/mypackage/libs/Request.php

namespace Http;
class Request {}

Upvotes: 3

Views: 3832

Answers (2)

İbrahim Duran
İbrahim Duran

Reputation: 378

Solved it by myself. I just had to reinstall package whenever I change pacakge's composer.json.

Upvotes: 1

Wouter J
Wouter J

Reputation: 41954

First of all, it's often better to use psr-0 or psr-4 autoloading config. With the classmap, you have to redump the autoloader each time you add a new class or rename one.

You always need to include the Composer autoloader by using require 'vendor/autoload.php';. The best place to add such require statement is in your front controller file.

Upvotes: 1

Related Questions