用户登录
用户注册

分享至

多个控制器的单个 Laravel 路由

  • 作者: 简简单单单
  • 来源: 51数据库
  • 2022-10-21

问题描述

我正在创建一个项目,其中我有多种用户类型,例如.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

感谢任何帮助:)

推荐答案

我能想到的最佳解决方案是创建一个控制器来管理用户的所有页面.

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');  
    }
}
软件
前端设计
程序设计
Java相关