dani3l
634
6
Laravel

Route groups inside a resource

Posted 3 years ago by dani3l

Hi guys I'm trying to create something like this: http://localhost/api/workspaces/1/templates/categories/1

workspaces is the root resource, templates is a route group with prefix, categories is another resource

right now i'm doing something like this in my routes file, which feels too hacky for me:

Route::group(['prefix' => 'api', middleware => 'api.auth'], function() {
    if (Request::is('api/workspaces/*')) {
        $workspace = Workspace::findOrFail(request()->segment(3));

         Route::group(['prefix' => 'workspaces/'.$workspace->getKey(), 'middleware' => 'user.workspace'], function() use($workspace) {
                Route::group(['prefix' => 'templates'], function() {
                    Route::resource('categories', 'CategoryController');
        });

        });
    }

    Route::resource('workspaces', 'WorkspaceController']);
});

Is there any other way to achieve this?

Please sign in or create an account to participate in this conversation.