单个Laravel路由用于多个控制器 [英] Single Laravel Route for multiple controllers

查看:332
本文介绍了单个Laravel路由用于多个控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个项目,其中有多个用户类型,例如.超级管理员,管理员,管理员等.用户通过身份验证后,系统会检查用户类型并将其发送到相应的控制器.中间件可以正常工作.

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' => 'App\Http\Middleware\SuperAdminMiddleware'], function () {
        Route::get('dashboard', 'SuperAdmin\dashboard@index'); // SuperAdmin Dashboard
        Route::get('users', 'SuperAdmin\manageUsers@index'); // SuperAdmin Users
    });

 // Admin Routes
    Route::group(['middleware' => 'App\Http\Middleware\AdminMiddleware'], function () {
        Route::get('dashboard', 'Admin\dashboard@index'); // Admin Dashboard
        Route::get('users', 'Admin\manageUsers@index'); // Admin Users
    });

我知道我们可以重命名诸如superadmin/dashboard和admin/dashboard之类的路由,但是我想知道是否还有其他方法可以实现干净的路由.有人知道有什么解决方法吗?

顺便说一句,我正在使用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路由用于多个控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆