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

mecjos's avatar

Naming Api Resources Routes

Hi. Is there a way to name api resources routes.. for example:

Route::prefix('admin')->middleware('auth:admin')->group(function () {
    Route::apiResources([
        '/users' => 'Admin\UserController',   => how to name this? *******
        '/plans' => 'Admin\PlanController' => how to name this ? ***********
    ]);
});

I want to do that because I have another controller with same name in root controller namespace.. Thus I get serialization error while executing route:cache artisan command.

Thank you.

0 likes
4 replies
Nihir's avatar

Hi @mecjos if you are using resource in your route and you already connect with the controller like PHP artisan make: model Test -mcr like this one then you don't have to put a specific name in there you have to just watch your rout list and you can find that there is already a name with the resource controller.

mecjos's avatar

@Nihir yes but I have another /plans route for another PlanController like as follows:

Route::middleware('auth:api')->group(function () {
    Route::apiResources([
        '/plans' => 'PlanController'
    ]);
});

one controller in App\Http\Controllers and other one is in App\Http\Controllers\Admin directory.. when I run route:cache command it gives error as this route name already exists..

Nihir's avatar

@mecjos For the error, you must specify which PlanController you are using. Like this:

Route::middleware('auth:api')->group(function () {
    Route::apiResources([
        '/plans' => 'App\Http\Controllers\PlanController'
    ]);
});

For specific Admin Route, you can create the admin.php file where you can use App\http\controllers\Admin\PlanController Or you can use it directly as follow:

Route::middleware('auth:api')->group(function () {
    Route::apiResources([
        '/plans' => 'App\Http\Controllers\Admin\PlanController'
    ]);
});

After this, you just have to include this file in your web.php.

And for your errors, I saw that you are not using Prefix, so travel sees all routes are the same for the solution. You can use Prefix in both routes, and I'm damm sure your issue will be solved.

For using Prefix like this:

Route::namespace('admin\plans')->group(function () {
    Route::resource('plans', 'App\Http\Controllers\Admin\PlanController'');
});

Route::namespace('plans')->group(function () {
    Route::resource('plans', 'App\Http\Controllers\PlanController'');
});

or there is one more solution, and I think this will help you to improve your code.

https://laravel-news.com/laravel-route-organization-tips

mecjos's avatar

@Nihir thanks for your reply.. but this also didn't work.. My controllers are already in different folder and I got same result with "namespace()" definition.. Using namespace('Admin') method before grouping is same thing with Admin/Controller/PlanController.. I think laravel use last word of route in order to name routes. My full routing example is as follows:

Route::namespace('Admin')->middleware('auth:admin')->group(function () {
    Route::apiResources([
        'admin/users' => 'UserController',
        'admin/plans' => 'PlanController'
    ]);
}); 

Route::middleware('auth:api')->group(function () {
    Route::apiResources([
        'plans' => 'PlanController'
    ]);
});

For plan controller api resource laravel tries to create same named route. Full error message I got :

Unable to prepare route [api/admin/plans] for serialization. Another route has already been assigned name [plans.index]. 

Please or to participate in this conversation.