Skip to content

Instantly share code, notes, and snippets.

@petersuhm
Created April 17, 2013 07:33
Show Gist options
  • Save petersuhm/5402443 to your computer and use it in GitHub Desktop.
Save petersuhm/5402443 to your computer and use it in GitHub Desktop.
Laravel 4 nested resources. I had some trouble finding a good way to add nested resources in my routes.php file. Therfore, I thought I would share what I ended up with. Please say so, if you have any improvements to this :)
<?php
class ConversationsController extends BaseController {
public function show($user_id, $conversation_id)
{
var_dump($user_id);
var_dump($conversation_id);
}
}
<?php
Route::group(array('before' => 'auth'), function()
{
Route::group(array('prefix' => 'users/{user_id}'), function()
{
Route::resource('conversations', 'ConversationsController', array('only' => array('show')));
});
});
@petersuhm
Copy link
Author

The URL to the show route: GET /users/1/conversations/43

@franzliedke
Copy link

Why do you have two nested route grous? Can't you add both the before filter and the prefix to the same group configuration?

@JonoB
Copy link

JonoB commented Apr 17, 2013

You also dont need to prefix like that unless you have a specific reason. The following will work:

Route::resource('users.conversions', 'ConversationsController');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment