5

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.

AppyGG
  • 381
  • 1
  • 6
  • 12
Serellyn
  • 405
  • 1
  • 9
  • 26

7 Answers7

8

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'));
Oleh Diachenko
  • 612
  • 1
  • 7
  • 9
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.

JoeGalind
  • 3,545
  • 2
  • 29
  • 33
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');
pr1nc3
  • 8,108
  • 3
  • 23
  • 36
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');
});
mrhn
  • 17,961
  • 4
  • 27
  • 46
0

Either remove this line:

use App\Http\Controllers\ApiController;

or add a \ to the start:

use \App\Http\Controllers\ApiController;
Rouhollah Mazarei
  • 3,969
  • 1
  • 14
  • 20
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.

endo64
  • 2,269
  • 2
  • 27
  • 34
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

CodeToLife
  • 3,672
  • 2
  • 41
  • 29