我已经通过以下方式解决了这个问题
routes.php
Route::get('dashboard', ['middleware' => 'auth', 'uses' => 'DashboardController@index']);
// Admin
Route::group([ 'middleware' => 'toegang:admin' ], function ()
{
Route::get('dashboard/projecten', 'ProjectController@index');
Route::get('dashboard/groepen', 'GroepController@index');
Route::get('dashboard/periode', 'PeriodeController@index');
Route::get('dashboard/producten', 'ProductController@index');
Route::get('dashboard/gebruikers', 'UserController@index');
Route::get('dashboard/scoring_rubrics', 'ScroingRubricsController@index');
Route::get('dashboard/pos', 'PosController@index');
Route::get('dashboard/project_status', 'ProjectStatusController@index');
Route::get('dashboard/beoordeling', 'BeoordelingController@index');
});
// Student
Route::group([ 'middleware' => 'toegang:student' ], function ()
{
Route::get('dashboard/project_status_student', 'ProjectStatusStudentController@index');
Route::get('dashboard/account', 'AccountStudentController@index');
});
我有多个角色管理员/学生,他们使用一个控制器 DashBoardController。
所以要访问仪表板,管理员或学生都必须是访问仪表板主页的身份验证。对于管理员和学生的特定页面,我使用两个路由组。
dashboardcontroller@index
class DashboardController extends Controller
{
/**
* Display a listing of the resource.
*
* @return View View
*/
public function index()
{
$autorisatie = Auth::user()->autorisatie();
return view('dashboard.home', compact('autorisatie'));
}
在仪表板控制器中,我将通过调用 autorisatie() 从用户那里获取角色
用户模型中的自动化方法
public function autorisatie($rol = null)
{
$autorisatie = DB::table('autorisatie')
->select('rol')
->where('autorisatieID', Auth::user()->autorisatieID)
->first();
// Check als de opgegeven role in de routes hetzelfde is
// Als de ingelogde user
if($rol)
{
// Return true als $autorisatie rol hetzelfde is als de
// opgegeven route role
return $autorisatie->rol == $rol;
}
// return false
return $autorisatie->rol;
}
在我看来,我会检查不同的显示数据或页面,如下所示:
查看仪表板
{{--Extends master page--}}@extends("master.master")
{{--Section for content area--}}
@section("content")
<h1>Dashboard</h1>
<p>
Dashboard - {{$autorisatie}} <br>
@if ($autorisatie == 'admin')
Show admin things.....
@elseif ($autorisatie == 'student')
Show student things...
@endif
</p>