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

Scottly's avatar

Unable to prepare route [widgets] for serialization. Another route has already been assigned name [widgets.index]

I've been trying to remove closures from my routes file so that I can implement route caching. Having completed this, I've now hit the following issue when running "php artisan route:cache":

Unable to prepare route [widgets] for serialization. Another route has already been assigned name [widgets.index]

Now, I assume that this is because I have a widgets.index route in both my web routes and in my API routes. What is the best practice for managing this situation? Can I exclude the API from route caching? Should I rename the API route to api.widgets.index or something? If so, is there an easy way to mass-rename routes? I have a few dozen routes at the moment with more to come.

0 likes
4 replies
bobbybouwmann's avatar

In general, the API routes don't need a name because you probably won't reference them in your controllers as a route. Your controllers only redirect to web routes. API routes always return some JSON response.

If you want to keep the name prefixing the API routes with api is indeed the best solution here.

1 like
Scottly's avatar
Scottly
OP
Best Answer
Level 4

To be clear, I hadn't named either web or API routes yet and I've been using resource routing with the default naming like so:

Route::resource('widgets', 'WidgetController');

and

Route::apiResource('widgets', 'API\WidgetsController');

I've fixed it now by updating my API routes to the following

Route::apiResource('widgets', 'API\WidgetsController', array("as" => "api"));

Routes are now caching successfully.

6 likes
shaiful's avatar

@Scottly Hi, Thank you this helped to solve my route caching issue. Anyway I know this is old but how did you get to that solution? Is there any documentation about what route options we can use when declaring the apiResource routes?

Thank you.

NicolasMica's avatar

👋 Hi there !

Nice to hear you solved you issue, please don't forget to mark the thread as solved as it may help other 🙏

It was indeed a naming overlap as Laravel automatically namspaces routes when using the resource and apiResource methods with the following pattern {resourceName}.{controllerAction}. In that specific case it means widgets.index|show|store|update|destroy were defined twice.

Please or to participate in this conversation.