Laravelny bullets on routes

I hope this turns out to be a simple situation that I just forgot in the documentation. I am refactoring our web application to use bullet in urls. Our company allows you to register many organizations, each of which has its own page and auxiliary pages. I am trying to do something like below:

Route::get('/{organization-slug}', 'OrganizationController@index');
Route::get('/{organization-slug}/{organization-subpage-slug}', 'OrganizationController@subpage');
Route::get('/', 'IndexController@index');
Route::get('/dashboard', 'DashboardController@index');

However, how can I do this without contradicting other routes? For example, if I have it '/{organization-slug}', it will also match any root level path. Therefore, if the user navigates to /dashboard, they will be redirected to OrganizationController@indexinsteadDashboardController@index

Does laravel have built-in functions to handle this situation?

EDIT

In response to some of the answers that say that the order of the route files is something that needs to be reviewed. I created a new laravel project to test this and added the following routes to/routes/web.php

Route::get('/{some_id}', function($some_id){
    echo $some_id;
});
Route::get('/{some_id}/{another_id}', function($some_id, $another_id){
    echo $some_id . ' - ' . $another_id;
});
Route::get('/hardcoded/subhard', function(){
    echo 'This is the value returned from hardcoded url with sub directory';
});
Route::get('/hardcoded', function(){
    echo 'This is the value returned from hardcoded url';
});

Routes /hardcoded/subhardand are /hardcodednever reached. When is this order used. However, if we move static routes above dynamic, as shown below:

Route::get('/hardcoded/subhard', function(){
    echo 'This is the value returned from hardcoded url with sub directory';
});
Route::get('/hardcoded', function(){
    echo 'This is the value returned from hardcoded url';
});
Route::get('/{some_id}', function($some_id){
    echo $some_id;
});
Route::get('/{some_id}/{another_id}', function($some_id, $another_id){
    echo $some_id . ' - ' . $another_id;
});

Then the corresponding routes work as expected. Is it correct?

+4
source share
4 answers

The order is important in the route file. Put the most common in the last.


Edited by:

Route::get('/', 'IndexController@index'); Route::get('/dashboard', 'DashboardController@index'); Route::get('/{organization-slug}/{organization-subpage-slug}', 'OrganizationController@subpage'); Route::get('/{organization-slug}', 'OrganizationController@index');

+1
source

Laravel , . Route::get('/dashboard', 'DashboardController@index'); :

Route::get('/{organization-slug}', 'OrganizationController@index');
Route::get('/{organization-slug}/{organization-subpage-slug}', 'OrganizationController@subpage');
Route::get('/dashboard', 'DashboardController@index');
Route::get('/', 'IndexController@index');
+2

You can use Regular Expression Constraints for your routes.

Add ->where('organization-slug', '^(?!.*dashboard).*$')at the end of your parameterized routes, and they will work for any slugs except the "dashboard", and the dashboard route will also work safely for http://yourdomain.com/dashboard.

Route::get('/{organization-slug}', 'OrganizationController@index')->where('organization-slug', '^(?!.*dashboard).*$');
Route::get('/{organization-slug}/{organization-subpage-slug}', 'OrganizationController@subpage')->where('organization-slug', '^(?!.*dashboard).*$');
Route::get('/dashboard', 'DashboardController@index');
Route::get('/', 'IndexController@index');

And if you have other routes, for example, a toolbar, you can add them as well.

Route::get('/{organization-slug}', 'OrganizationController@index')->where('organization-slug', '^(?!.*dashboard|.*dashboard1|.*dashboard2|.*dashboard3).*$');
+1
source

Put Route::get('/dashboard', 'DashboardController@index');at the beginning of the route file:

Route::get('/dashboard', 'DashboardController@index');
Route::get('/{organization-slug}', 'OrganizationController@index');
Route::get('/{organization-slug}/{organization-subpage-slug}', 'OrganizationController@subpage');
Route::get('/', 'IndexController@index');
0
source

Source: https://habr.com/ru/post/1685885/


All Articles