2
votes

If I have two route groups (for simple prefixing of routes) is it possible to Route::bind per just that group?

When I do the following:

Route::group( array('prefix'=>'pre1'), function(){
  Route::bind('items', function( $value, $route ){
    $item = Item::find( $value );
    if( !$item ) App::abort( 404 );
    return $item;
  })

  Route::resource('items', .... );
})

Route::group( array('prefix'=>'pre2'), function(){
  //put bind for users here...
  Route::bind('items', function( $value, $route ){
    $user_id = $route->parameter('users')->getAttribute('id');
    $item = Item::where('id', $value)->whereUserId( $user_id );
    if( !$item ) App::abort( 404 );
    return $item;
  })

  Route::resource('users.items', ....)
})

The first bind to 'items' is overridden by the last one declared. I would rename the 'items' to something else, but nested Resource routes are auto generated by laravel.

Ie the first route is

/items/{items}

where the second is

/users/{users}/items/{items}

I would simply rename the end routes, but they make sense with regards to the resources being used from an admin having permissions on one resource and users on the other.

1

1 Answers

4
votes

A couple of things. Firstly you dont need this code

  Route::bind('items', function( $value, $route ){
    $item = Item::find( $value );
    if( !$item ) App::abort( 404 );
    return $item;
  })

You just need

  Route::bind('items', 'Item')

It will automatically throw a 404 if it cant bind the Item model at runtime.

Secondly, you wont be able to do what you want (have two different bindings of the same name) - but there are two options.

Option 1 is just explicitly have all your routes defined in your routes file, and dont use Route::resource(). This article from Phil Sturgeon gives an excellent explanation of why you should just define each route manually.

The second Option is just to use the main Items route binding, but add a filter to the user items. Something like this:

Route::group( array('prefix'=>'pre2', 'before' => 'user.item'), function(){

Then define a filter which checks if the item belongs to the user

Route::filter('user.item', function($route, $request)
{
    if ($route->parameter('item')->user_id !== Auth::user()->id)
    {
        App::abort(404);
    }
});