zeropsi
zeropsi

Reputation: 694

Is there a different way I should be naming my Laravel routes?

I currently have these routes in my API:

Route::group(['prefix' => 'v1'], function() {
    Route::get('feed', [FeedController::class, 'index']);
    Route::get('feed/{page}', [FeedController::class, 'index']);
});

I wanted to create another route that looked like this, only protected by some middleware:

Route::group(['prefix' => 'v1', 'middleware' => ['jwt.verify']], function() {
    Route::get('feed/following', [FeedController::class, 'following']);
    Route::get('feed/following/{page}', [FeedController::class, 'following']);
});

However, it appears that the v1/feed/following route is actually being routed to the v1/feed/{page} route.

Is there a way to fix this without my route names?

Upvotes: 0

Views: 101

Answers (2)

bumperbox
bumperbox

Reputation: 10214

The rule for 'feed/{page}' will also match 'feed/following', you will wind up with page = following

Maybe create the route for 'feed/following' before the route for 'feed/{page}'

Upvotes: 3

Daniel Mesa
Daniel Mesa

Reputation: 586

You can introduce another group in your v1 Api path and set feed/following before feed/{page}:

Route::group(['prefix' => 'v1'], function() {
    Route::group(function() {
        Route::get('feed/following', [FeedController::class, 'following']);
        Route::get('feed/following/{page}', [FeedController::class, 'following']);
    })->middleware('jwt.verify');

    Route::get('feed', [FeedController::class, 'index']);
    Route::get('feed/{page}', [FeedController::class, 'show']);
});

Upvotes: 5

Related Questions