Home > Back-end >  Laravel Routes Group-Prefix using variable
Laravel Routes Group-Prefix using variable

Time:08-02

I'm trying to use a variable (which contains a value stored in the session) as a prefix to all my group routes, so I can make it more readable and clean.

Basically I want to transform this:

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');

});

Into something like this:

$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');
}

What's the correct way to do this?

CodePudding user response:

Can you try like this. It's worked on me.

Open app/Http/Providers/AppServiceProvider

use Illuminate\Support\Facades\Session;
public function boot()
{
$role = Session::get('role');
    app()->bind('role', function() use ($role){
        return [
                'role' => $role
        ];
     });
}

In routes/web.php

define('role', app()->make('role')['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'); });

In blade;

{{ route(app()->make('role')['role'].'.dashboard') }}

You can try to send "app()->make('role')['role']" as a variable to your blade for more understanding.

CodePudding user response:

Try this;

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

While using;

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'); });
  • Related