group

We sometimes need to group our routes to save extra coding more prefixes and one-by-one tagging out middleware auth. Route groupings is here to save you.

Route::group($param, $action);

$param : is an array that contains the prefix and the middleware of the group.

$action : is a function where we declare the routes inside.

For example, let's take a look with route grouping in our profile route.

Route::group(['prefix' => 'profile', 'middleware' => ['auth']], function () {
    Route::get("/", ['ProfileController@index']);
    Route::post('/', ['ProfileController@update']);
    Route::post('/changepass', ['ProfileController@changePass']);
    Route::post('/delete', ['ProfileController@delete']);
});

grouping is just like saying: (/profile, /profile/changepass, /profile/delete).

First we need to define a prefix to our group by saying ['prefix' => 'your-desired-prefix'] and we define the middleware saying ['middleware' => ['auth']]. Basically when saying ['middleware' => ['auth']] all the routes the we define inside the group will be protected by auth. If you are not authenticated you cannot access this defined routes.

Last updated