简体   繁体   中英

Laravel Route pass additional variables

I have a user controller which returns users list, each user belongs to different group.

ex:- $groups = ['student_users' => 1, 'teacher_users' => 2, .....]

I can create routes such as below to access them

Route::get('users/{id}', [
        'as'            =>  'user',
        'uses'          =>  'Admin\UserController@listUser'
    ]);

But i want to create more user friendly or seo friendly say like this

Route::get('users/student', [
        'as'            =>  'student',
        'uses'          =>  'Admin\UserController@listUser'
    ]);

Route::get('users/teacher', [
        'as'            =>  'teacher',
        'uses'          =>  'Admin\UserController@listUser'
    ]);
Route::get('users', [
        'as'            =>  'student',
        'uses'          =>  'Admin\UserController@listUser'
    ]);//by default shows students list.

And i want to pass the id via route not via url. Whats the best way to do it.

do as following

Route::get('users/student', [
    'as'            =>  'student',
    'type'          =>  'student',
    'uses'          =>  'Admin\UserController@listUser'
]);

in controller you can get type as below

public function listUser(\Illuminate\Http\Request $request)

   $action = $request->route()->getAction();
   dd($action['type']); 
}

type is just an example. You can pass any variable.

I hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM