问题描述:
我正在尝试使用一个变量(其中包含一个存储在会话中的值)作为我所有组路由的前缀,这样我就可以使它更具可读性和简洁性。
基本上我想改变这个:
- Route::group(['prefix' => 'admin', 'middleware' => ['auth', 'roles', 'PreventBackHistory']], function() {
- Route::get('dashboard', 'App\Http\Controllers\RolesController@index')->name('admin.dashboard');
- Route::get('profile', 'App\Http\Controllers\RolesController@profile')->name('admin.profile');
- Route::get('editRegs', 'App\Http\Controllers\RolesController@editRegs')->name('admin.edit');
- Route::get('settings', 'App\Http\Controllers\RolesController@settings')->name('admin.settings');
-
- });
-
- Route::group(['prefix' => 'user', 'middleware' => ['auth', 'roles', 'PreventBackHistory']], function() {
- Route::get('dashboard', 'App\Http\Controllers\RolesController@index')->name('user.dashboard');
- Route::get('profile', 'App\Http\Controllers\RolesController@profile')->name('user.profile');
- Route::get('settings', 'App\Http\Controllers\RolesController@settings')->name('user.settings');
-
- });
-
- Route::group(['prefix' => 'manager', 'middleware' => ['auth', 'roles', 'PreventBackHistory']], function() {
- Route::get('dashboard', 'App\Http\Controllers\RolesController@index')->name('manager.dashboard');
- Route::get('profile', 'App\Http\Controllers\RolesController@profile')->name('manager.profile');
- Route::get('settings', 'App\Http\Controllers\RolesController@settings')->name('manager.settings');
-
- });
变成这样:
- $role = session()->get('role');
- Route::group(['prefix' => $role, 'middleware' => ['auth', 'roles', 'PreventBackHistory']], function() {
- Route::get('dashboard', 'App\Http\Controllers\RolesController@index')->name($role.'.dashboard');
- Route::get('profile', 'App\Http\Controllers\RolesController@profile')->name($role.'.profile');
- Route::get('editRegs', 'App\Http\Controllers\RolesController@editRegs')->name('admin.edit');
- Route::get('settings', 'App\Http\Controllers\RolesController@settings')->name($role.'.settings');
- }
这样做的正确方法是什么?
解决思路一:
尝试这个
define('role', session()->get('role'));
使用时
- Route::group(['prefix' => $role, 'middleware' => ['auth', 'roles', 'PreventBackHistory']], function() {
- Route::get('dashboard', 'App\Http\Controllers\RolesController@index')->name(role.'.dashboard');
- Route::get('profile', 'App\Http\Controllers\RolesController@profile')->name(role.'.profile');
- Route::get('editRegs', 'App\Http\Controllers\RolesController@editRegs')->name('admin.edit');
- Route::get('settings', 'App\Http\Controllers\RolesController@settings')->name(role.'.settings'); });
解决思路二(这是解决小编问题的思路):
以上仅为部分解决思路,添加下方公众号后回复001,即可查看全部内容。公众号有许多评分最高的编程书籍和其它实用工具,无套路,可放心使用
如果您觉得有帮助,可以关注公众号——定期发布编程科技相关的资讯和资源