简体   繁体   English

Laravel Route传递其他变量

[英]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, .....] 例如: - $ 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 但我想创建更多用户友好或seo友好的说法像这样

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. 我想通过路由而不是通过url传递id。 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 在控制器中,您可以获得如下type

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

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

type is just an example. type只是一个例子。 You can pass any variable. 您可以传递任何变量。

I hope it helps. 我希望它有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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