Support the ongoing development of Laravel.io →
Requests Architecture Configuration

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);
Last updated 2 years ago.
0

Or is the method that I outlined pretty much what should be used?

0

Sign in to participate in this thread!

Eventy

Your banner here too?

mseymour mseymour Joined 25 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.