C
C
cheremsha112019-05-08 12:15:43
PHP
cheremsha11, 2019-05-08 12:15:43

How to specify the full path to the class in composer.json?

Example:
There is a global class Test in the vendor/mylib/Test.php folder
In vendor/mylib/composer.json I specified

"autoload": {
    "psr-4": {
      "Test": "/Test.php"
    }
  },

but class "Test" not found after Composer autoloader generation

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Aksentiev, 2019-05-08
@Sanasol

Because / is an absolute path from the system root
Plus Test is a namespace, not a class. And the output will be Test / Test, and not just Test.
To simply register classes, you need to do something like

"autoload": {
    "psr-4": {
      "": "src/"
    }
  },

or
"autoload": {
    "classmap": ["src/"]
}

But it's better not to do that, namespaces don't just exist.

D
Dmitry, 2019-05-08
@slo_nik

Good afternoon.
Specify the path where the files are located in composer.json, and write namespace in the files themselves.
For example, there is a core directory where the Test.php class is located.
The core directory and composer.json are on the same level.
So, in composer.json you specify

"autoload": {
      "psr-4": {
         "app\\": ""
       }
 }

In the Test.php class, specify the namespace
namespace app\core;
class Test
{
  //******
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question