問題描述
我正在創建一個項目,其中我有多種用戶類型,例如.superadmin、admin、managers 等.一旦用戶通過身份驗證,系統會檢查用戶類型并將其發送到相應的控制器.用于此的中間件工作正常.
I am creating a project where i have multiple user types, eg. superadmin, admin, managers etc. Once the user is authenticated, the system checks the user type and sends him to the respective controller. The middle ware for this is working fine.
所以當經理去 http://example.com/dashboard 時,他會看到經理儀表板,而當管理員去到他可以看到管理儀表板的同一個鏈接.
So when manager goes to http://example.com/dashboard he will see the managers dashboard while when admin goes to the same link he can see the admin dashboard.
下面的路線組單獨工作正常,但放在一起時只有最后一個工作.
The below route groups work fine individually but when placed together only the last one works.
/***** Routes.php ****/
// SuperAdmin Routes
Route::group(['middleware' => 'AppHttpMiddlewareSuperAdminMiddleware'], function () {
Route::get('dashboard', 'SuperAdmindashboard@index'); // SuperAdmin Dashboard
Route::get('users', 'SuperAdminmanageUsers@index'); // SuperAdmin Users
});
// Admin Routes
Route::group(['middleware' => 'AppHttpMiddlewareAdminMiddleware'], function () {
Route::get('dashboard', 'Admindashboard@index'); // Admin Dashboard
Route::get('users', 'AdminmanageUsers@index'); // Admin Users
});
我知道我們可以重命名路由,如超級管理員/儀表板和管理員/儀表板,但我想知道是否還有其他方法可以實現干凈的路由.有沒有人知道任何解決方法?
順便說一句,我使用的是 LARAVEL 5.1
感謝任何幫助:)
推薦答案
我能想到的最佳解決方案是創建一個控制器來管理用戶的所有頁面.
The best solution I can think is to create one controller that manages all the pages for the users.
routes.php 文件中的示例:
example in routes.php file:
Route::get('dashboard', 'PagesController@dashboard');
Route::get('users', 'PagesController@manageUsers');
您的 PagesController.php 文件:
your PagesController.php file:
protected $user;
public function __construct()
{
$this->user = Auth::user();
}
public function dashboard(){
//you have to define 'isSuperAdmin' and 'isAdmin' functions inside your user model or somewhere else
if($this->user->isSuperAdmin()){
$controller = app()->make('SuperAdminController');
return $controller->callAction('dashboard');
}
if($this->user->isAdmin()){
$controller = app()->make('AdminController');
return $controller->callAction('dashboard');
}
}
public function manageUsers(){
if($this->user->isSuperAdmin()){
$controller = app()->make('SuperAdminController');
return $controller->callAction('manageUsers');
}
if($this->user->isAdmin()){
$controller = app()->make('AdminController');
return $controller->callAction('manageUsers');
}
}
這篇關于多個控制器的單個 Laravel 路由的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!