我通常在 app 目录下的Laravel中添加了我的课程 . 今天,当我添加一个具有目录结构的API客户端时,我无法在Laravel PSR-4默认结构中识别出一个类 .

我的API客户端目录结构是:

app -- MySubdirectory -- Partners +
                                  |- Partner1
                                  |- Partner2 +
                                              |- Includes -- (other subdirectories)
                                              |- Version -- (other subdirectories)
                                              |- ApiClient.php

现在,我已经创建了一个位于app下方的模型MyModel .

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class MyModel extends Model
{
    /**
     */
    protected $api;

    /**
     * Returns MyModel API Client connected to the API.
     */
    public function __construct()
    {
        parent::__construct();

        // ApiClient is not recognized thought PhpStorm do recognized.
        $this->api = \ApiClient::factory(PROTOCOL_JSON, VERSION_DEFAULT);

        $credentials = json_decode(OtherModel::where('name', 'some_name')->configuration);

        $this->api->setConnectId($credentials['connect_id']);
        $this->api->setSecretKey($credentials['secret_key']);
    }
}

好吧,当我写完MyModel时

$this->api = \ApiClient::factory(PROTOCOL_JSON, VERSION_DEFAULT);

PhpStorm建议上课,显然是认识到自动加载的课程 .

但是当我运行脚本时,我收到错误:

FatalThrowableError in MyModel.php line 20: 
Class 'ApiClient' not found

好吧,也许是 composer autoload 的一些问题 - 甚至直到现在我已经创建的其他类被 PHP 自动识别 .

所以,我运行了 composer dump-autoload -o 并验证了 autoload_classmap.php 文件,确实识别了 app 目录下的所有其他子目录/类,减去 MySubdirectory 以及 MySubdirectory 以下的其他子目录和类 .

我的composer.json文件有基本的自动加载部分:

"autoload": {
    "classmap": [
        "database"
    ],
    "psr-4": {
        "App\\": "app/"
    }
},

那么,为什么运行 composer dump-autoload 无法捕获 MySubdirectory 以及下面的其他子目录和类?

为什么PhpStorm会 grab ApiClient.php