I'm hoping this turns out to be a simple situation that I have just overlooked in the documentation. I am refactoring our web application to utilize slugs in urls. Our company allows many organizations to register, each having their own page and sub pages. I am trying to accomplish something like the 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 conflicting with other routes? For example if I have '/{organization-slug}'
this would also match for any root level route. So if a user goes to /dashboard
, they would be routed to OrganizationController@index
instead of DashboardController@index
Does laravel have built in functionality to handle this situation?
EDIT
In response to some of the answers stating that the order of the routes file is what needs to be revised. I have 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';
});
The routes /hardcoded/subhard
and /hardcoded
are never reached. When this order is used. However, if we move the static routes above the dynamic like 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 appropriate routes appear to be working as expected. Is this correct?
A slug is the part of a URL which identifies a page using human-readable keywords.
They can be used to mask ID's from your database table. For example, if you didn't want to expose the id of an article, e.g: /articles/1 , you could use a slug instead /articles/this-is-my-article.
Order is important in route file. Put the most generic in last.
Edited:
Route::get('/', 'IndexController@index'); Route::get('/dashboard', 'DashboardController@index'); Route::get('/{organization-slug}/{organization-subpage-slug}', 'OrganizationController@subpage'); Route::get('/{organization-slug}', 'OrganizationController@index');
Laravel considers the last route definition for the same resource as the valid route. So just put the Route::get('/dashboard', 'DashboardController@index');
after the definition of the route of slugs:
Route::get('/{organization-slug}', 'OrganizationController@index');
Route::get('/{organization-slug}/{organization-subpage-slug}', 'OrganizationController@subpage');
Route::get('/dashboard', 'DashboardController@index');
Route::get('/', 'IndexController@index');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With