4
votes

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.

6
Capitalization matters - is there a file called app/Http/Controllers/ApiController.php? Or is it perhaps APIController.php?ceejayoz
Why not just use Route::get('/base', 'ApiController@base')->name('base');Foued MOUSSI
I believe, 'uses' => .... add the default namespace . You can do this instead [ApiController::class , 'base']Clément Baconnier

6 Answers

3
votes

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'));
1
votes

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.

1
votes

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');
1
votes

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');
});
0
votes

Either remove this line:

use App\Http\Controllers\ApiController;

or add a \ to the start:

use \App\Http\Controllers\ApiController;
0
votes

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.