0

I've got (what is to me) an interesting url question.

This is my situation. I have what will be a user populated database, so I cannot be sure how many subareas I will have.

I will always have an area, one or more subareas, and a location that ends my url.

example: /area/subarea1/subarea2/location

This is slightly simplified from what I need. I need to be able to service the following urls as well;

/area/subarea1/location

/area/subarea1/subarea2/subarea3/location)

My routes look something like this:

Route::get('area/{subarea1}', 'SubareaController@show');
Route::get('area/{subarea1}/{location}', 'LocationController@show');
Route::get('area/{subarea1}/{subarea2}', 'SubareaController@show2');
Route::get('area/{subarea1}/{subarea2}/{location}', 'LocationController@show2');

So the problem here is that my routes are overriding each other, because they are essentially the same.

My question is this. Is there any way to differentiate these routes when they have the same url structure? And if not, is there a better way to handle multiple subareas between an area, and a location?

EDIT

Ok I've been tried naming my routes, but I can't seem to be able to use the named routes correctly with all my parameters in the view. I may look into the area/{subarea1}/subarea1/{subarea2}/subarea2 solution, even though I would rather not have the longer URL.

1
  • What about naming your routes? Commented Jul 8, 2016 at 6:24

1 Answer 1

2

This happens because Laravel has no way to distinguish each route from the other. For example, it would route these 2 url's to the same action:

example.com/area/my-subarea-1/my-location
example.com/area/my-subarea-1/my-subarea-2

So you need different paths. Try this:

Route::get('area/subarea1/{subarea1}', 'SubareaController@show');
Route::get('area/subarea1/{subarea1}/location/{location}', 'LocationController@show');
Route::get('area/subarea1/{subarea1}/subarea2/{subarea2}', 'SubareaController@show2');
Route::get('area/subarea1/{subarea1}/subarea2/{subarea2}/location/{location}', 'LocationController@show2');
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, this solution seems to be working. I may go back later and try the named routing solution to see if I can get a smaller url, as in it's current form it's quite unwieldy.
Naming the routes is always recommended because it helps you reference your routes across your app without worrying about them being changed someday, but it won't solve this specific issue. The url's would still be the same and overwrite each other.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.