Search code examples
restrouteslaravelcrudcontrollers

restful controller inside controller with laravel


I'm having a designcontest website where my contests controller to show a single contest is as follows:

Route::get('contests/(:any)', array('as' => 'contest', 'uses' => 'contests@show'));

This will create a url like designcontest.com/contests/123

With the id of the current contest shown as '123' here.

User can submit entries to a single contest wich they can delete aswell so I need another set of routes with belonging to an entries controller that is basically inside the single contest view.

Jeffrey Way likes to refer to http://guides.rubyonrails.org/routing.html#crud-verbs-and-actions as a guide on how to name routes. This shows a good example on how to name your routes but what about the case I'm making here?

Should my url to create a new contestentry belonging to a certain contest be something like:

designcontest.com/contests/123/entries/new

I'm guessing this would be a good approach but I don't know what is usually being done in this case. So basically I'm asking what is the best url structure to follow in the case of a controller(entries) that is part of another controller(contests).


Solution

  • I think you're in the right path and I do exactly like you're thinking of doing. Take a look at the child nested resources on rails: http://guides.rubyonrails.org/routing.html#nested-resources, they also look exactly like yours: /magazines/:magazine_id/ads/new.