I have a set of routes where they only contain model-binded parameters and no intermediary text, except for one:
/areas — AreaController@index
/{areas} — LocationController@index
/{areas}/{locations} — ItemController@index
/{areas}/{locations}/{item} — ItemController@show
Everything but {item}
are slugs. Obviously I want to constrain the rightmost parameter so then it would only work if the second rightmost has a relationship with the rightmost, and so on going leftwards.
The reason why it is set up this way is because:
/areas
would show the areas in the database/{areas}
would show locations that are associated with that area/{areas}/{locations}
should show items from a specific location from a specific area, and/{areas}/{locations}/{item}
would show the specific item from the specific location from the specific area... plus http://example.com/newfoundland-and-labrador/st-johns/100 is a lot shorter than having something like http://example.com/areas/newfoundland-and-labrador/locations/st-johns/item/100.
Is there a better way of simplifying my controller setup and constraining the model-binded routes other than having separate that would only have one function (index
) and having a guard pattern at the top of functions like (taken from my "item" controller's show method, lightly edited)
public function show(Area $area, Location $location, Item $item)
{
if($item->location_id != $location->id || $location->area_id != $area->id)
abort(404);
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community