A
A
Annywebart2016-05-03 03:59:07
Laravel
Annywebart, 2016-05-03 03:59:07

Laravel 5.2 - how to write dynamic routes correctly?

Hello!
Site with nested pages (4 levels) and products.
Pages and products are stored in different tables (pages and products).
Moreover, the product and the page can be on the same level.
I wrote routes like this:

Route::get('{parentOne}/{parentTwo}/{parentThree}/{page}', ['uses' => '[email protected]']);
Route::get('{parentOne}/{parentTwo}/{page}', ['uses' => '[email protected]']);
Route::get('{parentOne}/{page}', ['uses' => '[email protected]']);
Route::get('{page}', ['uses' => '[email protected]']);

Route::get('{categoryOne}/{categoryTwo}/{categoryThree}/{product}', ['as' => 'product.productInfo', 'uses' => '[email protected]']);
Route::get('{categoryOne}/{categoryTwo}/{product}', ['as' => 'product.productInfo', 'uses' => '[email protected]']);
Route::get('{categoryOne}/{product}', ['as' => 'product.productInfo', 'uses' => '[email protected]']);

But, of course, when viewing the product, we get 404 (not found in Page), because. we simply do not get into product routes.
Please tell me how to do it right, I really don’t want to write nonsense!

Answer the question

In order to leave comments, you need to log in

6 answer(s)
A
Andreo, 2016-05-03
@chupacabramiamor

Good morning.
There is a very handy thing - Route::controller
Used in the file with your routes like this:
Where ProductController is your controller to which routes will be dynamically attached.
For example:
In the controller it is expressed as:

public function getSomeCategory($id)
{
    // ...
}

Thus, your nestings can be added indefinitely:
public function getSomeCategory($id, $subid, $subid, $subid)
{
    // ...
}

Matching URL:
Something like this...

N
Nokhchi Borz, 2016-05-03
@CheeBorz

https://laravel.com/docs/5.2/routing

W
WebDev, 2016-05-03
@kirill-93

This won't work.

{parentOne}/{parentTwo}/{parentThree}/{page}
и 
{categoryOne}/{categoryTwo}/{categoryThree}/{product}

This is the same. You set 4 variables in the route, and there are 4 variables there and there, Laravel will work on the first match.

S
Stanislav Pochepko, 2016-05-03
@DJZT

There are filters for routes by patterns in the documentation. If you have pages in the form of a slug and categories in the form of an id, then you can apply filters and specify forcibly that there are only numbers, and there, respectively, only + characters at the end of the number. (For the uniqueness of the slug)

R
Rustamka Vorontsov, 2016-05-03
@rmfordev

read about Optional Parameters and it will be much easier.

Y
Yaroslav, 2020-06-23
@ManMadeMind

Judging by the date, it is probably already very late with the answer. But now I had exactly the same problem, when I clicked on dynamic urls, laravel6 gave out 404. But, everything was solved very simply for me, I used Cyrillic in the url. Check if you also have some of the meanings of Cyrillic characters, or some other non-Latin characters.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question