How to make multiple routes share the same URL structure or URL parameters

October 26, 2018

If you have the following URLs:

/control-panel/{team_id}/

/control-panel/{team_id}/schedule

/control-panel/{team_id}/delete

etc...

(They all share the first part of the url - /control-panel/{team_id})

Then you don't have to write out the routes like this:

<?php
Route::get("control-panel/{team_id}","TeamController@index");
Route::get("control-panel/{team_id}/schedule","TeamController@schedule");
Route::delete("control-panel/{team_id}/delete","TeamController@destroy");

Instead you can group the routes and add a prefix:

<?php
Route::group(["prefix"=>"control-panel/{team_id}"], function() {
    Route::get("/","TeamController@index");
    Route::get("schedule","TeamController@schedule");
    Route::delete("delete","TeamController@destroy");
});

Putting a group within a group

You can also put a Route::group() inside another one.

<?php
Route::group(["prefix"=>"control-panel/{team_id}"], function() {
    Route::get("/","TeamController@index");
    Route::group(["prefix"=>"schedule"],function() {
        Route::get("/","TeamController@schedule");
        Route::post("/add_new","TeamController@store");
    }
    Route::delete("delete","TeamController@destroy");
});