问题描述
我正在创建一个项目,其中我有多种用户类型,例如.super*****、*****、managers 等.一旦用户通过身份验证,系统会检查用户类型并将其发送到相应的控制器.用于此的中间件工作正常.
i am creating a project where i have multiple user types, eg. super*****, *****, 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 ***** goes to the same link he can see the ***** dashboard.
下面的路线组单独工作正常,但放在一起时只有最后一个工作.
the below route groups work fine individually but when placed together only the last one works.
/***** routes.php ****/ // super***** routes route::group(['middleware' => 'apphttpmiddlewaresuper*****middleware'], function () { route::get('dashboard', 'super*****dashboard@index'); // super***** dashboard route::get('users', 'super*****manageusers@index'); // super***** users }); // ***** routes route::group(['middleware' => 'apphttpmiddleware*****middleware'], function () { route::get('dashboard', '*****dashboard@index'); // ***** dashboard route::get('users', '*****manageusers@index'); // ***** users });
我知道我们可以重命名路由,如超级管理员/仪表板和管理员/仪表板,但我想知道是否还有其他方法可以实现干净的路由.有没有人知道任何解决方法?
顺便说一句,我使用的是 laravel 5.1
感谢任何帮助:)
推荐答案
我能想到的最佳百家乐凯发k8的解决方案是创建一个控制器来管理用户的所有页面.
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 'issuper*****' and 'is*****' functions inside your user model or somewhere else if($this->user->issuper*****()){ $controller = app()->make('super*****controller'); return $controller->callaction('dashboard'); } if($this->user->is*****()){ $controller = app()->make('*****controller'); return $controller->callaction('dashboard'); } } public function manageusers(){ if($this->user->issuper*****()){ $controller = app()->make('super*****controller'); return $controller->callaction('manageusers'); } if($this->user->is*****()){ $controller = app()->make('*****controller'); return $controller->callaction('manageusers'); } }