Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel slugs in routes

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?

like image 212
whitwhoa Avatar asked Sep 18 '17 13:09

whitwhoa


People also ask

What is slug in route?

A slug is the part of a URL which identifies a page using human-readable keywords.

Why do we use slug in Laravel?

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.


2 Answers

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');

like image 200
Mohammad Hassany Avatar answered Sep 21 '22 22:09

Mohammad Hassany


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');
like image 43
Jean Marcos Avatar answered Sep 21 '22 22:09

Jean Marcos