A. Volg
A. Volg

Reputation: 307

Laravel: Target class does not exist. But it does

I've searched for answers, but mostly the problem was a typo in class or a controller.

In my case everything is spelled properly. Class in app\Http\Controllers\GenerateTextController.php:

<?php 
      namespace App\Http\Controllers;

      class generateText extends Controller
      {
           public function generate()
           {
              dd('success');
           }
      }

then I try to inject it into blade. home.blade.php :

  @inject ('generate', 'App\Http\Controllers\GenerateTextController')
  @dd($generate)

Result:

Target class [App\Http\Controllers\GenerateTextController] does not exist. 

I have already composer autoloaded couple of times, artisan cache cleared, nothing helps. I can't even find a closest solution in web.

Interesting thing: When I try dd on the other class, that was, how to say, "predefined" by Laravel - it shows the class. And my second custom class can be viewed in browser with such injection.

Any help appreciated.

Upvotes: 1

Views: 2908

Answers (2)

The Alpha
The Alpha

Reputation: 146191

Your class file name is app\Http\Controllers\GenerateTextController.php but the class name is generateText, that's the problem. The class name and the file name should match. This is how, PSR-4 autoloader works.

From the Specification:

The terminating class name corresponds to a file name ending in .php. The file name MUST match the case of the terminating class name.

So the class name should be like:

class GenerateTextController extends Controller
{
    // ...
}

Read about PSR-4 autoloader to understand it.

Upvotes: 3

mrhn
mrhn

Reputation: 18926

Classes file name should be the same as the PHP class name.

class GenerateTextController extends Controller

Upvotes: 2

Related Questions