6

For some reason, which is probably my fault, Laravel thinks it should be looking for the class ApiController in path: 'App\Http\Controllers\App\Http\Controllers', so... it doubles, but I have no idea why.

It's a brand new Laravel 6 project, I've created the ApiController with the make:controller artisan command and added a function, like this:

namespace App\Http\Controllers; use Illuminate\Http\Request; class ApiController extends Controller { public function base() { return 'This is a test function'; } } 

Then I've added a route to the api routes like this:

use App\Http\Controllers\ApiController; use Illuminate\Http\Request; use Illuminate\Support\Facades\Route; Route::group(['prefix' => '/v1', 'as' => 'api'], function () { Route::get('/base', ['uses' => ApiController::class . '@base'])->name('base'); }); 

As you can see, I've even 'imported' the controller, but it just can't find it. That's it, no other files or changes to the project. Also tried clearing route cache and dump-autoload, but that did not change anything.

3
  • Capitalization matters - is there a file called app/Http/Controllers/ApiController.php? Or is it perhaps APIController.php? Commented Jan 14, 2020 at 14:15
  • Why not just use Route::get('/base', 'ApiController@base')->name('base'); Commented Jan 14, 2020 at 14:17
  • 2
    I believe, 'uses' => .... add the default namespace . You can do this instead [ApiController::class , 'base'] Commented Jan 14, 2020 at 14:18

7 Answers 7

10

In my case problew was, in RouteServiceProvider, in using routes Namespace

protected $namespace = 'App\Http\Controllers'; 

In Laravel 8 namespace was commented out, i remove namespace from chain, because my web routes not fully moved to Laravel 8 syntax and i need this namespace.

 Route::prefix('api') ->middleware('api') -̶>̶n̶a̶m̶e̶s̶p̶a̶c̶e̶(̶$̶t̶h̶i̶s̶-̶>̶n̶a̶m̶e̶s̶p̶a̶c̶e̶)̶ ->group(base_path('routes/admin-api.php')); 
Sign up to request clarification or add additional context in comments.

1 Comment

This was the problem for me.
1

If you wanna ::class reference in the router, it should be done like this.

Route::group(['prefix' => '/v1', 'as' => 'api'], function () { Route::get('base', [ApiController::class, 'base'])->name('base'); }); 

Comments

1

This should work:

Route::group(['prefix' => '/v1', 'as' => 'api'], function () { Route::get('base', 'ApiController@base')->name('base'); }); 

No need to add the "use", since controllers are referenced from the App/Controllers namespace, as you can corroborate on the RouteServiceProvider.

Comments

1

The syntax of your route is a combination of "old syntax" vs "new syntax"

What you are trying to achieve is:

Route::get('/base', [ApiController::class, 'base'])->name('base'); 

Comments

0

Either remove this line:

use App\Http\Controllers\ApiController; 

or add a \ to the start:

use \App\Http\Controllers\ApiController; 

Comments

0

In my case (Laravel 8 project), I needed a separate route for destroy, because deleting didn't use html form, so my web.php file is like:

use App\Http\Controllers\LocationController; ... Route::resource('/locations', LocationController::class); Route::get('/locations/destroy/{location}', [LocationController::class, 'destroy']); 

But in that case if I put use App\Http\Controllers\LocationController the first line (Route::resource...) fails, if I remove it then the second line fails. So I removed use line and added App\Http\Controllers into the second line:

Route::resource('/locations', LocationController::class); Route::get('/locations/destroy/{location}', [App\Http\Controllers\LocationController::class, 'destroy']); 

So obviously Laravel doesn't automatically add App\Http\Controllers in the second form of Route.

Comments

0

I got this error when in resource controller description me pasted one from fresh project:

Route::resources([ 'my_url' => LisseyDoruHisobotController:class, ..., //other controllers ]); 

as being a recommended signature in Laravel 8 , but am currently busy on 7 or 6 version, where should be:

Route::resources([ 'my_url' => 'path\to\LisseyDoruHisobotController', ..., //other controllers ]); 

otherwize it will show doubled path

Laravel Target class [App\Http\Controllers\App\Http\Controllers\ ] does not exist

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.