Answer the question
In order to leave comments, you need to log in
How to make friends with two Routes?
Recently started learning Laravel. The task was to write a Route to intercept with the brand name,
say
test.ru/brand1
test.ru/brand2
, etc.
But at the same time, pages such as /about, /contacts, etc. (which may have a different template) should work.
What I did now:
1) Wrote route. It intercepts everything (I haven’t written a regular season yet, I grab everything)
Route::get('/{id}', [BrandController::class, 'brandItem'])->name('brand');
public function brandItem($id){
$brand = new Brand;
$brand_item = $brand->where('url', $id)->first();
return $brand_item ? $brand_item : false;
}
Route::get('/{id}', [BrandController::class, 'brandItem'])->name('brand');
Route::get('/about', function () {
return view('about');
});
Answer the question
In order to leave comments, you need to log in
Swap routes.
They are processed in order.
In the controller, I look for the brand in the database and, if successful, I give
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question