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

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

  • 相关阅读:
    sklearn包中对于分类问题,如何计算accuracy和roc_auc_score?
    PyTorch入门教学——dir()函数和help()函数的应用
    云计算:开辟数字时代的无限可能
    MySQL:查询时进行时间比较
    因为写保护,U盘会“假死”。如何在Windows 10上删除写保护
    微信支付回调,内网穿透详细过程
    面食有哪些 面食的种类大全
    第十期|惊!游戏广告主投放十万被骗,推广作弊竟全是虚拟用户
    CSS-0_2 CSS和继承(inherit & initial)
    JZ49 [剑指 Offer 49] 丑数
  • 原文地址:https://blog.csdn.net/qq_38334677/article/details/126119702