简体   繁体   中英

namespaces not being autoloaded by composer update

I have external repository written in vanilla PHP which I want to use in my Symfony project the 'composer require' call works fine and all the files are loaded into the vendor directory. At run time I keep getting the following error.

Attempted to load class "FM" from namespace "MyRepo1\Src\Vendors\FM".
Did you forget a "use" statement for another namespace?

I thing it may be the composer.json of the PHP repository, included below.

{
    "name" : "myUserName/MyRepo1",
    "minimum-stability" : "dev",
    "repositories" : [{
            "type" : "git",
            "url" : "https://github.com/myUserName/MyRepo2"
        }
    ],
    "require" : {
        "myUserName/MyRepo2" : "*",
        "php" : "^7.0"
    },
    "version" : "1.0.2",
    "require-dev" : {
        "phpunit/phpunit" : "5.5.*",
        "squizlabs/php_codesniffer" : "2.*",
        "mayflower/php-codebrowser" : "~1.1"
    }
}

below is the FM class which is in the directory vendor/myUserName/MyRepo1/src/vendors tracking.interface and tracking.class are in the same directory

<?php
namespace MyRepo1\Src\Vendors\FM;
// : Includes
include_once (dirname(realpath(__FILE__)) . DIRECTORY_SEPARATOR .    'tracking.interface');
include_once (dirname(realpath(__FILE__)) . DIRECTORY_SEPARATOR . 'tracking.class');
// : End
use MyRepo1\Src\Vendors\Vendors as Vendors;
use MyRepo1\Src\Vendors\TrackingInterface as TrackingInterface;

class FM extends Vendors\Vendors implements TrackingInterface\TrackingInterface
{

}

Are there any changes that I need to make to the composer.json file so that the classes will be autoloaded correctly with a composer update .

we eventually solved this by adding

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

to the composer.json and changed the namespace of the classes to namespace Vendors; so that it would be the same as the directory.

根据命名空间的写入方式,文件应位于

MyRepo1\Src\Vendors\FM\FM.php;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM