3
votes

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 {}
2
If you come here in 2019, all you need to do is composer dumpautoloadJavier S

2 Answers

1
votes

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.

1
votes

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