Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 {}
like image 594
İbrahim Duran Avatar asked Mar 04 '26 14:03

İbrahim Duran


2 Answers

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.

like image 84
Wouter J Avatar answered Mar 06 '26 03:03

Wouter J


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

like image 43
İbrahim Duran Avatar answered Mar 06 '26 02:03

İbrahim Duran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!