Home > database >  Laravel. Conflict when building routes
Laravel. Conflict when building routes

Time:11-21

There are two routes:

Route::get('/{article:slug}', [ArticleController::class, 'showArticlePage']);

and

Route::get('/{user:nickname}', [ProfileInfoController::class, 'getUserByNickname']);

Is there any way for each of the routes to perform its function?You can't change uri

For example:

  1. domain.com/nickname => I have to get the user There is a search in the table "users"

2.domain.com/my-first-article => I have to get the article There is a search in the table "articles"

Note that each routes has its own controller and action, but they have a similar uri

CodePudding user response:

You need something to distinguish them it'll be a lot more helpful. E.g use an @ in front of usernames.

Another approach would be if you know for sure all slugs will have a hyphen, then you can chain ->where('slug', '...')

See https://pineco.de/handy-regex-constraints-in-laravel-routes/

Otherwise, it'll go through the first defined route.

  • Related