0

I'm new in laravel.I am using entrust Roles and permission which is working fine, but now I need to add every role like admin has his own controller and view. Superadmin should have his own controller and view.

Can anyone please help me? How to access permission based controller and view using entrust.

This is how I tried:

    Route::group(['middleware' => ['auth']], function() {
 if(Entrust::hasRole('superadmin')) {
            return View::make('superadmin');
        }
        else if(Entrust::hasRole('Admin')) {
            return View::make('admin');
        }
        else {
            Auth::logout();
            return Redirect::to('/login')
                ->with('flash_notice', 'You don\'t have access!');
        }


    Route::get('/home', 'HomeController@index');

    Route::resource('users','UserController');

    Route::get('roles',['as'=>'roles.index','uses'=>'RoleController@index','middleware' => ['permission:role-list|role-create|role-edit|role-delete']]);
    Route::get('roles/create',['as'=>'roles.create','uses'=>'RoleController@create','middleware' => ['permission:role-create']]);
    Route::post('roles/create',['as'=>'roles.store','uses'=>'RoleController@store','middleware' => ['permission:role-create']]);
    Route::get('roles/{id}',['as'=>'roles.show','uses'=>'RoleController@show']);
    Route::get('roles/{id}/edit',['as'=>'roles.edit','uses'=>'RoleController@edit','middleware' => ['permission:role-edit']]);
    Route::patch('roles/{id}',['as'=>'roles.update','uses'=>'RoleController@update','middleware' => ['permission:role-edit']]);
    Route::delete('roles/{id}',['as'=>'roles.destroy','uses'=>'RoleController@destroy','middleware' => ['permission:role-delete']]);
Tarvo Mäesepp
  • 4,477
  • 3
  • 44
  • 92
Asad Javed
  • 29
  • 7

1 Answers1

0

you should use Entrust middlewares in your routes.php. add "superadmin" routes in "superadmin" middleware, "admin" routes in "admin" middleware, etc. and put the "else" case after all these.

Route::group(['middleware' => ['role:superadmin']], function() {
    Route::get('/someroute', function(){
        return View::make('superadmin');
    });
});

Route::group(['middleware' => ['role:admin']], function() {
    Route::get('/someroute', function(){
        return View::make('admin');
    });
});

Route::get('/someroute', function(){
    Auth::logout();
        return Redirect::to('/login')
            ->with('flash_notice', 'You don\'t have access!');
});
Fatemeh Majd
  • 719
  • 8
  • 22
  • i will check your answer , but i have a question in my mind, why we didn't use Entrust::hasRole to check user role name? Fatemeh – Asad Javed Aug 25 '16 at 19:49
  • generally, there shouldn't be such logics via if/else in routes file. this file should have only the routing duty. but as why it didn't work?. honestly, I'm not sure, as I haven't seen this kind of logic in routes file before. but I think the reason should be that we can't use special classes like Entrust in routes file. have you noticed there is no import statement at the top? and general rules usually don't apply to this special file. – Fatemeh Majd Aug 25 '16 at 19:58
  • i have tried your code , but not Redirect to specific Route, ... what should i do? – Asad Javed Aug 25 '16 at 20:33
  • i want first check current user,then redirect to his controller like SuperAdmin controller or shopAdmin controller – Asad Javed Aug 26 '16 at 04:36
  • change the get route to `Route::get('/someroute', 'ShopController@function')` to use controllers instead of views. – Fatemeh Majd Aug 26 '16 at 14:02