Laravel authorization will help you to do this http://laravel.com/docs/5.1/authorization
Nov 11, 2015
3
Level 3
How do you redirect users based on user roles
Currently I'm working on a project that requires several different user groups to have different view. What I thought I could do is have a middleware direct the users after login to the right view but I can't seem to get it to work.
I'm going to post my route file to hopefully explain what im trying to do.
<?php
/*Root page*/
Route::get('/home', ['as' => 'homepage', 'uses' => 'HomeController@index']);
/*News page*/
Route::get('/news', 'NewsController@index');
// Authentication routes...
Route::get('auth/login', 'Auth\AuthController@getLogin');
Route::post('auth/login', 'Auth\AuthController@postLogin');
Route::get('auth/logout', 'Auth\AuthController@getLogout');
// Registration routes...
Route::get('auth/register', 'Auth\AuthController@getRegister');
Route::post('auth/register', 'Auth\AuthController@postRegister');
//Special users routes
Route::group(['middleware' => ['auth','role:special'],
'namespace' => 'special',
'as' => 'special.'], function ()
{
Route::get('/', ['uses' => 'DashboardController@index', 'as' => 'dashboard.home']);
Route::get('survey_analysis',
['uses' => 'ResultsController@index', 'as' => 'survey_analysis.index']);
Route::get('survey_analysis/report',
['uses' => 'ResultsController@report', 'as' => 'survey_analysis.report']);
//The show method could also be the manage survey page.
Route::resource('survey', 'SurveyController',
['only' => ['index', 'show', 'create', 'store']]);
Route::get('survey/{id}/manage', ['uses' => 'SurveyController@manage',
'as' => 'survey.manage']);
//can they view others profiles?
Route::get('/profile', ['uses' => 'ProfileController@show', 'as' => 'profile.show']);
Route::get('/profile/edit', ['uses' => 'ProfileController@edit', 'as' => 'profile.edit']);
Route::patch('/profile', ['uses' => 'ProfileController@update', 'as' => 'profile.update']);
});
//basic user's routes
Route::group(['middleware' => ['auth','role:basic'],
'namespace' => 'basic',
'as' => 'basic.'], function ()
{
Route::get('/dashboard', ['uses' => 'DashboardController@index', 'as' => 'dashboard.home']);
Route::get('/profile', ['uses' => 'ProfileController@show', 'as' => 'profile.show']);
Route::get('/profile/edit', ['uses' => 'ProfileController@edit', 'as' => 'profile.edit']);
Route::patch('/profile', ['uses' => 'ProfileController@update', 'as' => 'profile.update']);
Route::get('/survey/{id}', ['uses' => 'SurveyController@show', 'as' => 'survey.show']);
Route::post('/survey', ['uses' => 'SurveyController@show', 'as' => 'survey.store']);
Route::get('/results', ['uses' => 'ResultsController@index', 'as' => 'results.index']);
Route::get('survey/{id}/results', ['uses' => 'ResultsController@show', 'as' => 'results.show']);
});
//admin user routes
// Authentication routes...
Route::group(['prefix' => 'admin',
'namespace' => 'admin'], function ()
{
Route::get('auth/login', 'AuthController@getLogin');
Route::post('auth/login', 'AuthController@postLogin');
Route::get('auth/logout', 'AuthController@getLogout');
// Registration routes...
Route::get('auth/register', 'AuthController@getRegister');
Route::post('auth/register', 'AuthController@postRegister');
});
Route::group(['middleware' => ['role:admin'],
'namespace' => 'admin',
'prefix' => 'admin',
'as' => 'admin.'], function ()
{
Route::get('/', ['uses' => 'DashboardController@index', 'as' => 'dashboard.home']);
Route::get('survey_analysis',
['uses' => 'ResultsController@index', 'as' => 'survey_analysis.index']);
Route::get('survey_analysis/report',
['uses' => 'ResultsController@report', 'as' => 'survey_analysis.report']);
//The show method could also be the manage survey page.
Route::resource('survey', 'SurveyController',
['only' => ['index', 'show', 'create', 'store']]);
Route::get('survey/{id}/manage', ['uses' => 'SurveyController@manage',
'as' => 'survey.manage']);
//can they view others profiles?
Route::get('/profile', ['uses' => 'ProfileController@show', 'as' => 'profile.show']);
Route::get('/profile/edit', ['uses' => 'ProfileController@edit', 'as' => 'profile.edit']);
Route::patch('/profile', ['uses' => 'ProfileController@update', 'as' => 'profile.update']);
//Route::get('company/{id}/profile', ['uses' => 'CompanyProfile@show', 'as' => 'company.profile.show']);
//Route::get('company/{id}/profile', ['uses' => 'CompanyProfile@update', 'as' => 'company.profile.update']);
});
Please or to participate in this conversation.