未找到Laravel服务提供商类

时间:2018-05-31 16:31:31

标签: php laravel service-provider

我正在使用Laravel 5.4我正在创建一个服务提供程序,仅用于测试。我的目标是创建一个TestClass的实例,并让它在测试控制器中说出Hello World。我已经成功注册了我的服务提供程序,但是当我尝试实例化我的TestClass时,我得到了一个Class not found错误。

配置/ app.php

Test\Providers\MyTest\TestServiceProvider::class,

TestServiceProvider.php

namespace Test\Providers\MyTest;

use Illuminate\Support\ServiceProvider;
use Test\Services\TestClass;

class TestServiceProvider extends ServiceProvider
{
    public function boot()
    {

    }
    public function register()
    {
        $this->app->bind('Test\Services\TestClass', function ($app) {
          return new TestClass();
        });

    }
}

TestClass.php

namespace Test\Services;

class TestClass
{

    public function SayHi()
    {
        return "Hello World";
    }
}

TestController.php

...
use Test\Services\TestClass;
...

class TestController extends Controller
{
    public function serviceProviderTest()
    {
        $words = 'words';
        $testClass = new TestClass();
        $words = $testClass->SayHi();

        return view('test.serviceProviderTest', array(
            'words' => $words
        ));
    }
    ...
}

这给了我以下错误:

  

FatalThrowableError类' Test \ Services \ TestClass'找不到

如果我发表评论

// $testClass = new TestClass();
// $words = $testClass->SayHi();

我没有错误,我看到'字'在我看来如预期的那样。

为什么我的TestClass无法找到?

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:2)

将新的基本路径添加到"psr-4": { "App\\": "app/", "Test\\": "test/" },

composer dump-autoload

然后运行:

...
Bitmap flag = new Bitmap(200, 100);
Graphics flagGraphics = Graphics.FromImage(flag);
int red = 0;
int white = 11;
while (white <= 100) {
    flagGraphics.FillRectangle(Brushes.Red, 0, red, 200,10);
    flagGraphics.FillRectangle(Brushes.White, 0, white, 200, 10);
    red += 20;
    white += 20;
}
...