在 Laravel 中如果要 autoload 自訂的 library,必須透過 Laravel 的 composer.json

架構

└── app
    └── lib
        └── FallZu
              └── Test.php

Test.php

namespace FallZu;

class Test
{
    public static function sayHello()
    {
        echo 'Hello' . PHP_EOL;
    }
}

composer.json (加入 psr-0)

"autoload": {
    "classmap": [
        "app/commands",
        "app/controllers",
        "app/models",
        "app/database/migrations",
        "app/database/seeds",
        "app/tests/TestCase.php"
    ],
    "psr-0": {
        "FallZu": "app/lib"
    }
}

更新 Autoload

composer dump-autoload
Categories: Laravel