Answer the question
In order to leave comments, you need to log in
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]']);
Answer the question
In order to leave comments, you need to log in
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)
{
// ...
}
public function getSomeCategory($id, $subid, $subid, $subid)
{
// ...
}
This won't work.
{parentOne}/{parentTwo}/{parentThree}/{page}
и
{categoryOne}/{categoryTwo}/{categoryThree}/{product}
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)
read about Optional Parameters and it will be much easier.
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 questionAsk a Question
731 491 924 answers to any question