E
E
Evgeniy Golovin2021-06-25 18:44:24
Laravel
Evgeniy Golovin, 2021-06-25 18:44:24

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');

2) In the controller, I look for the brand in the database and, if successful, I give
public function brandItem($id){
        $brand = new Brand;
        $brand_item = $brand->where('url', $id)->first();

        return $brand_item ? $brand_item : false;
}

Question: what to do so that in the case of false, there would be a check of other Routes?

Route::get('/{id}', [BrandController::class, 'brandItem'])->name('brand');

Route::get('/about', function () {
    return view('about');
});

Tell me which way to look, maybe there is another option. Of course, it comes to mind right in the Brenda controller to write in which case where to go, but it resembles a "crutch." Let's say if there are 10 such pages, then everything should be listed in the controller.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey delphinpro, 2021-06-25
@JonGol

Swap routes.
They are processed in order.

In the controller, I look for the brand in the database and, if successful, I give

If unsuccessful, return 404

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question