• Laravel Routes Group-Prefix 使用变量问题


    问题描述:

    我正在尝试使用一个变量(其中包含一个存储在会话中的值)作为我所有组路由的前缀,这样我就可以使它更具可读性和简洁性。

    基本上我想改变这个:

    1. Route::group(['prefix' => 'admin', 'middleware' => ['auth', 'roles', 'PreventBackHistory']], function() {
    2. Route::get('dashboard', 'App\Http\Controllers\RolesController@index')->name('admin.dashboard');
    3. Route::get('profile', 'App\Http\Controllers\RolesController@profile')->name('admin.profile');
    4. Route::get('editRegs', 'App\Http\Controllers\RolesController@editRegs')->name('admin.edit');
    5. Route::get('settings', 'App\Http\Controllers\RolesController@settings')->name('admin.settings');
    6. });
    7. Route::group(['prefix' => 'user', 'middleware' => ['auth', 'roles', 'PreventBackHistory']], function() {
    8. Route::get('dashboard', 'App\Http\Controllers\RolesController@index')->name('user.dashboard');
    9. Route::get('profile', 'App\Http\Controllers\RolesController@profile')->name('user.profile');
    10. Route::get('settings', 'App\Http\Controllers\RolesController@settings')->name('user.settings');
    11. });
    12. Route::group(['prefix' => 'manager', 'middleware' => ['auth', 'roles', 'PreventBackHistory']], function() {
    13. Route::get('dashboard', 'App\Http\Controllers\RolesController@index')->name('manager.dashboard');
    14. Route::get('profile', 'App\Http\Controllers\RolesController@profile')->name('manager.profile');
    15. Route::get('settings', 'App\Http\Controllers\RolesController@settings')->name('manager.settings');
    16. });

    变成这样:

    1. $role = session()->get('role');
    2. Route::group(['prefix' => $role, 'middleware' => ['auth', 'roles', 'PreventBackHistory']], function() {
    3. Route::get('dashboard', 'App\Http\Controllers\RolesController@index')->name($role.'.dashboard');
    4. Route::get('profile', 'App\Http\Controllers\RolesController@profile')->name($role.'.profile');
    5. Route::get('editRegs', 'App\Http\Controllers\RolesController@editRegs')->name('admin.edit');
    6. Route::get('settings', 'App\Http\Controllers\RolesController@settings')->name($role.'.settings');
    7. }

    这样做的正确方法是什么?

    解决思路一:

    尝试这个

    define('role', session()->get('role'));

    使用时

    1. Route::group(['prefix' => $role, 'middleware' => ['auth', 'roles', 'PreventBackHistory']], function() {
    2. Route::get('dashboard', 'App\Http\Controllers\RolesController@index')->name(role.'.dashboard');
    3. Route::get('profile', 'App\Http\Controllers\RolesController@profile')->name(role.'.profile');
    4. Route::get('editRegs', 'App\Http\Controllers\RolesController@editRegs')->name('admin.edit');
    5. Route::get('settings', 'App\Http\Controllers\RolesController@settings')->name(role.'.settings'); });

    解决思路二(这是解决小编问题的思路):

    以上仅为部分解决思路,添加下方公众号后回复001,即可查看全部内容。公众号有许多评分最高的编程书籍和其它实用工具,无套路,可放心使用

    如果您觉得有帮助,可以关注公众号——定期发布编程科技相关的资讯和资源

  • 相关阅读:
    day49 jdbc技术
    加速智能驾驶项目落地?你还缺一套真值测评系统
    华为机试 - 工号不够用了怎么办?
    [Linux] 网络协议之TCP/IP五层模型
    【深度学习】clip-interrogator clip docker 容器启动过程
    goLang context组件学习笔记
    质量小议14 -- DevOps
    02. 板载音频功能
    棋牌室电脑计时灯控,棋牌室计时灯控安装,佳易王计时计费管理系统软件
    [CSS入门到进阶] 用transform后z-index失效了?总结transform的注意事项!
  • 原文地址:https://blog.csdn.net/qq_38334677/article/details/126119702