Laravel PSR-4不会自动加载类

时间:2014-04-04 17:31:38

标签: php laravel laravel-4

我在app目录中使用自定义文件夹,其中包含Billing,Exceptions,Repositories等。这是我的目录结构......

app
  -iw
     -Billing
         -BillingInterface.php
         -StripeBilling.php
     -Exceptions
     -Repositories
     -macros.php

composer.json

"psr-4": {
        "iw\\" : "app/iw"
    },

运行命令composer dump-autoload -o

// app/iw/BillingInterface.php (location)

<?php namespace iw\Billing;

interface BillingInterface {}

// app/iw/StripeBilling.php (location)

<?php namespace iw\Billing;

class StripeBilling {

    public function bill()
    {
        dd('billing');
    }

}

我发现找不到类错误,即使我注意到vendor/composer/autoload_ps4.php没有使用这个新文件夹进行更新。请帮忙。感谢

2 个答案:

答案 0 :(得分:2)

添加&#34; app / iw&#34;在&#34; classmap&#34;。

"autoload": {
    "classmap": [
        "app/commands",
        "app/controllers",
        "app/models",
        "app/database/migrations",
        "app/database/seeds",
        "app/tests/TestCase.php",
        "app/iw"
    ],
    "psr-4" : {
        "iw\\" : "app/iw"
    }
}

并运行

 composer dump-autoload -o

答案 1 :(得分:0)

我试过这个json文件并且它有用......

"autoload": {
        "psr-4": {"iw\\" : "app/iw" },
        "classmap": [
            "app/commands",
            "app/controllers",
            "app/models",
            "app/database/migrations",
            "app/database/seeds",
            "app/tests/TestCase.php"
        ]
    }

但请在自动加载部分中添加"psr-4": {"iw\\" : "app/iw" }之后建议原因。