reindert-vetter / api-version-control

A Laravel package to manage versions of endpoints in an elegant way

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Another route is already using that name

VFAndrew opened this issue · comments

Given documented example for defining api routes without version prefix in addition to versioned routes.

Route::middleware(['api', ApiVersionControl::class])
    ->prefix('api')
    ->group(base_path('routes/api.php'));

Route::middleware(['api', ApiVersionControl::class])
    ->prefix('api/{version}')
    ->where(['version', '#[a-z]\d{1,3}#'])
    ->group(base_path('routes/api.php'));

I was unable to cache my named routes as it was warning me that the name was already in use. I mitigated this issue by defining a group as name.

Route::middleware(['api', ApiVersionControl::class])
    ->prefix('api')
    ->as('default.')
    ->group(base_path('routes/api.php'));

Route::middleware(['api', ApiVersionControl::class])
    ->prefix('api/{version}')
    ->where(['version', '#[a-z]\d{1,3}#'])
    ->group(base_path('routes/api.php'));

Oh, but that could be a problem. You would want to check the name in your application. Then you should check 2 names ('*.users' would unintentionally lead to other matches):

if ($request->routeIs('default.users', 'users')) {
    //
}

I think we have to redirect all urls without version number to /v1 (instead of defining the routes twice). I'm going to think about this some more.

I've tried a few things, but haven't been able to come up with an elegant solution yet. I think I'll put your solution in the readme for now.

I've tried a few things, but haven't been able to come up with an elegant solution yet. I think I'll put your solution in the readme for now.

Not sure what is the right way for the package, but just thought I'd bring it to your attention in case someone else encountered it as well. As long as it is documented I believe the developers can knowingly take whatever action works for them.

This might only really be an issue for existing APIs trying to add versioning since they might need their original endpoints to still receive calls from existing third party integrations.

Yes, that is true. And you can always solve it in nginx configuration. I'll just document it. Thanks for your input!

Readme modified