Y
Y
Yuri Smith2017-07-26 11:08:17
Laravel
Yuri Smith, 2017-07-26 11:08:17

How to do routing in Laravel?

Hello. When creating the site, there were originally routes for categories and subcategories:

Route::pattern('category', '[a-zA-Z0-9\-]+'); // Такие паттерны у всех роутов
Route::get('/{auto}/{country}/', ['as' => 'country', 'uses' => '[email protected]']);
Route::get('/{auto}/{country}/archive/', ['as' => 'country.archive', 'uses' => '[email protected]']);
Route::get('/{auto}/{country}/{mark}/', ['as' => 'mark', 'uses' => '[email protected]']);
Route::get('/{auto}/{country}/{mark}/archive/', ['as' => 'mark.archive', 'uses' => '[email protected]']);

In the controller, if there is no data, then I call App:: abort (404); When I added new routes, the old ones stopped working (a 404 error was issued), they are written higher than the previous ones.
Route::get('/{category}/', ['as' => 'category.list', 'uses' => '[email protected]']); 
Route::get('/{category}/{article_slug}/', ['as' => 'article', 'uses' => '[email protected]']);

So I had a question how this can be avoided, I have already used standard functions like link_to_route, link_to_action but still nothing. The code:
<?php echo link_to_route('category.list', 'obuchenie', ['obuchenie'] ); ?>
<?php echo link_to_action('[email protected]', 'obuchenie2', ['obuchenie'] ) ?>
<a href="{{route('category.list',['obuchenie']) }}">obuchenie3</a>

It turns out it flips to another route
Route::get('/{page}/', ['as' => 'page', 'uses' => [email protected]']);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Talalaev, 2017-07-26
@nixxels

How does he know {auto} is different from {page} or {category}? The answer is no, since they are identical, so auto always works.
You need to come up with either a Route::prefix('auto') prefix (of type auto_{auto}), or write a naming rule for each pattern (to variables that form the path, ), or write a handler inside that, if it does not find auto, according to the rules, it will be thrown on page, etc.).
In short, study https://laravel.com/docs/5.4/routing , it is possible to paint your own router logic, but this will complicate everything.
I think your option is to switch to prefixes and everything will work beautifully and quickly.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question