1

I want to load HomeController class from lib directory:

root/
-lib/
--/HomeController.php
-vendor/
-composer.json
-index.php

Composer.json

"autoload": {
    "psr-4": {
        "Lib\\": "lib/"
    }
}

HomeController.php

namespace Lib;
class HomeController {}

index.php

var_damp(new \Lib\HomeController.php);

It doesn't find the class.
But if I put HomeController.php inside Controllers directory:

root/
-lib/
--/Controllers/HomeController.php

And update the namespaces: index.php to var_damp(new \Lib\Controllers\HomeController.php); and HomeController.php to:

namespace Lib\Controllers;
class HomeController {}

It works perfect.
It's weird, I can't find any docs talking about it. I don't need additional directories, in this case I want the HomeController class directly inside lib directory.

How can I make it works inside lib folder?

Doc999tor
  • 190
  • 3
  • 15

1 Answers1

0

I think the trailing slash in the path reference is your problem. Change the autoload section in composer.json to this:

"autoload": {
  "psr-4": {
    "Lib\\": "lib"
  }
}

...then run composer dump-autoload.

csknk
  • 1,899
  • 1
  • 16
  • 27
  • Tried it a few times. It doesn't have any effect. In Composer docs they advise to use the trailing slash. Anyway I'll try it again. Thanks – Doc999tor Mar 31 '17 at 14:14