• 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,即可查看全部内容。公众号有许多评分最高的编程书籍和其它实用工具,无套路,可放心使用

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

  • 相关阅读:
    JTS: 17 DiscreteHausdorffDistance 豪斯多夫距离计算
    【华为OD机试真题 python】完全二叉树非叶子部分后序遍历-2【2022 Q4 | 200分】
    JAVA计算机毕业设计电视设备租借系统Mybatis+系统+数据库+调试部署
    Idea本地跑flink任务时,总是重复消费kafka的数据(kafka->mysql)
    vue全局方法plugins/utils
    半个月时间把MySQL重新巩固了一遍,梳理了一篇几万字 “超硬核” 文章!
    ubuntu 安装docker
    Mybatis执行器BatchExecutor、ReuseExecutor、SimpleExecutor介绍
    【American English】美语口语,浊化,弱读,连读,省音
    目标检测新SOTA:YOLOv9问世,新架构让传统卷积重焕生机(附代码)
  • 原文地址:https://blog.csdn.net/qq_38334677/article/details/126119702