Miguel Stevens
Miguel Stevens

Reputation: 9201

Nested Controllers and routes

I'm creating a dashboard where the user can create Clients

Each Client will have: Categories, Employees, ... Now I'm wondering how to structure the routes.

For example if I create the following: (pseudo code)

Route::get('clients/{id}/');
Route::get('clients/{id}/categories');
Route::get('clients/{id}/categories/{category}');
Route::get('clients/{id}/categories/{category}/questions/{question}');

This seems like a lot of unnecessary parameters..

How do you guys go about this? I really want to split the categories on a seperate page, the employees on a seperate page.

Thank you.

Upvotes: 0

Views: 200

Answers (3)

Mouhcine Mahfoud
Mouhcine Mahfoud

Reputation: 94

in all of my projects, i avoid using a lot of nested entities in the URL, so i access each one apart, this was also recommended by @jeffrey_way at Laracasts, the training website for laravel. so, i would do the following:

Route::get('clients/{id}/');
Route::get('categories/{client_id}');
Route::get('category/{category}'); // Note that I have removed the plural s from categories
Route::get('question/{question}');

Good luck

Upvotes: 1

Imran
Imran

Reputation: 4750

Here in such case I would rather prefer to use only one route will all parameters as in GET method. So, I would have add just one param as bellow:

Route::get('client/create', 'ClientController@store');

So, all the parameters will be maintained by the store method of ClientController like below:

public function store(Request $request){
    $category = $request->get('category')
    //......
    //get other get parameters like this when required
}

When I need to trigger this route I would just do something like below:

<a href="{{url('client/create').'?category=1&question=2'}}">Create link</a>

As you know, we can pass our parameters here using our old global friend GET variable.

Upvotes: 0

Ian
Ian

Reputation: 3676

It honestly depends on how big your application is going to become, I would probably group them, so still keeping the same structure.

Route::group('clients/{id}', function()
{

    Route::get('/');

    Route::group('categories', function()
    {

        Route::get('/');
        Route::get('{category}');
        Route::get('{category}/questions/{question}');
    })
})

Same as yours but I feel it a little cleaning for later if you expand on the categories or clients.

Upvotes: 1

Related Questions