Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

reaz's avatar
Level 5

Proper syntax for declaring api resource route

Hi i am trying declare a api resource route with array syntax like this https://laravel.com/docs/8.x/controllers#api-resource-routes

Route::apiResource('segments', SegmentController::class);

But this does not work and i get an strange error saying it can not find the class. But in the error the path to class is duplicated. My class is at Project\Http\Controllers\SW\V1\SegmentController but laravel looking to find the class in Project\Http\Controllers\SW\V1\Project\Http\Controllers\SW\V1\SegmentController . I am not sure what i am doing wrong. If anyone can help with any insight that would be very helpul. Thanks in advance.

0 likes
7 replies
johnDoe220's avatar

write your api routes on api.php, for example

use App\Http\Controllers\Api\ApiController;
Route::get('employees', [ApiController::class, 'index']);
reaz's avatar
Level 5

@johnDoe220 , For project structure it is not possible to write it api.php. We have several route files for different parts of the API

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Check your RouteServiceProvider. You might have the namespace specified in there as well.

reaz's avatar
Level 5

@Sinnbeck , yes that was the problem. So i cant really use the new syntax on the resource route if my namespace is different.

Sinnbeck's avatar

@reaz I suggest you pick one. Either use the old method of using strings in route files and let the service provider handle the namespace, or use the new method of always using FooController::class and import the class

reaz's avatar
Level 5

@Sinnbeck , we were slowly turning to the new method since it makes code navigation easy in the ide. Most of the time bug fix starts like looking at the route file , then going to that controller to find the problem. New syntax works for single route though with the even with namespace defined in the RouteServiceprovider.

Sinnbeck's avatar

@reaz ok good to know. I changed all of mine in one go and just made sure my tests returned 100% success

Please or to participate in this conversation.