Reputation: 1355
I am using Laravel 5.4 I am creating a Service Provider just for testing. My goal is create an instance of my TestClass and make it say Hello World in a Test Controller. I have successfully registered my service provider, but when I try to instantiate my TestClass I get a Class not found error.
config/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
));
}
...
}
This gives me the following error:
FatalThrowableError Class 'Test\Services\TestClass' not found
If I comment out
// $testClass = new TestClass();
// $words = $testClass->SayHi();
I get no errors and I see 'words' in my view as expected.
Why can't my TestClass be found?
Any help would be greatly appreciated!
Upvotes: 0
Views: 2894
Reputation: 12867
Add the new base path into your composer.json
:
"psr-4": {
"App\\": "app/",
"Test\\": "test/"
},
Then run:
composer dump-autoload
Upvotes: 3