Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
483 views
in Technique[技术] by (71.8m points)

php - Single Laravel Route for multiple controllers

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.

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
    });

I know we can rename the routes like superadmin/dashboard and admin/dashboard but i was wondering if there is any other way to achieve the clean route. Does anyone know of any anywork arounds ?

BTW i am using LARAVEL 5.1

Any help is appreciated :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can do this with a Before Middleware that overrides the route action's namespace, uses and controller attributes:

use Closure;
use IlluminateHttpRequest;
use IlluminateContractsContainerContainer;
use AppHttpMiddlewareAdminMiddleware;
use AppHttpMiddlewareSuperAdminMiddleware;

class AdminRoutingMiddleware
{
    /**
     * @var Container
     */
    private $container;

    public function __construct(Container $container)
    {
        $this->container = $container;
    }

    private static $ROLES = [
        'admin' => [
            'namespace' => 'Admin',
            'middleware' => AdminMiddleware::class,
        ],
        'super' => [
            'namespace' => 'SuperAdmin',
            'middleware' => SuperAdminMiddleware::class,
        ]
    ];

    public function handle(Request $request, Closure $next)
    {
        $action = $request->route()->getAction();
        $role = static::$ROLES[$request->user()->role];

        $namespace = $action['namespace'] . '\' . $role['namespace'];

        $action['uses'] = str_replace($action['namespace'], $namespace, $action['uses']);
        $action['controller'] = str_replace($action['namespace'], $namespace, $action['controller']);
        $action['namespace'] = $namespace;

        $request->route()->setAction($action);

        return $this->container->make($role['middleware'])->handle($request, $next);
    }
}

This way you have to register each route only once without the final namespace prefix:

Route::group(['middleware' => 'AppHttpMiddlewareAdminRoutingMiddleware'], function () {
    Route::get('dashboard', 'dashboard@index');
    Route::get('users', 'manageUsers@index');
});

The middleware will convert 'dashboard@index' to 'Admindashboard@index' or 'SuperAdmindashboard@index' depending on current user's role attribute as well as apply the role specific middleware.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...