Reputation: 301
In Laravel, if I've two or more routes like:
Route::get('/{username}', 'UsersController');
Route::get('/{album}', 'AlbumsController');
and a user requested a page like www.example.com/foo
, then how will Laravel figure out if the user has opted for either for the user profile or the album's page.
Upvotes: 0
Views: 106
Reputation: 39389
Why not just structure URLs how they’re intended to be? A resource name and identifier (i.e. albums/{album_id}
).
What happens if a user creates an album with the same name as a user? How is your application going to differentiate between the two? What if you add other object types (like an event). It’s impossible to assume every object in your application is going to have a non-conflicting name.
Stop trying to go against the grain and just structure your URLs as per conventions. It’ll save you a lot of headaches in development.
Upvotes: 0
Reputation: 6386
Its possible to do what you want but it's really bad practice. It will confuse the user and yourself after you look at the code a few months later.
#btw, this is in laravel 5
Route::any('foo', function($verb)
{
switch($verb)
{
case 'album':
return view('somepage');
case 'user':
return view('some_user');
default:
return view('randompage');
}
});
i might of done the code wrong but did it without testing. But you get the point.
EDIT: just tested it, not possible. besides its a bad idea.
Upvotes: 0
Reputation: 60048
It wont - it is impossible for Laravel to know which - so you cant do that.
You need to make your routes
Route::get('/user/{username}', 'UsersController');
Route::get('/album/{album}', 'AlbumsController');
The only other way you might do it is if maybe there is no username matching foo
- you could have it try for an album called foo
- but then you run into other issues - like what if a username and album have the same name...
Upvotes: 5