0

再会;

我是 Laravel 的新手,我正在构建一个拥有多个用户(学生、主管、HOD)的网站。我正在使用该laratrust软件包来管理我的用户。例如,如果学生尝试访问属于主管的 URL,laratrust则会引发“用户没有任何必要的访问权限”的异常。我想将学生重定向回他们的主页,而不是这个例外。

这是我的路线:

web.php

// Routes For Students
Route::group([
    'name' => 'student.',
    'prefix' => 'student',
    'namespace' => 'App\Http\Controllers',
    'middleware' => ['auth', 'role:student']
], function () {
    Route::get('home', 'StudentController@index')->name('student-home');
    Route::get('proposal', 'StudentController@proposal')->name('student-proposal');
    Route::get('thesis', 'StudentController@thesis')->name('student-thesis');
});

// Routes For Supervisors
Route::group([
    'name' => 'supervisor.',
    'prefix' => 'supervisor',
    'namespace' => 'App\Http\Controllers',
    'middleware' => ['auth', 'role:supervisor']
], function () {
    Route::get('home', 'supervisorController@index')->name('supervisor-home');
    Route::get('proposal', 'supervisorController@proposal')->name('supervisor-proposal');
    Route::get('thesis', 'supervisorController@thesis')->name('supervisor-thesis');
});

// Routes For HOD
Route::group([
    'name' => 'hod.',
    'prefix' => 'hod',
    'namespace' => 'App\Http\Controllers',
    'middleware' => ['auth', 'role:hod']
], function () {
    Route::get('home', 'hodController@index')->name('hod-home');
    Route::get('proposal', 'hodController@proposal')->name('hod-proposal');
    Route::get('thesis', 'hodController@thesis')->name('hod-thesis');
});

我研究过,为了做到这一点,我需要编辑laratrust.php文件的这个特定部分:

    'middleware' => [
        'register' => true,
        'handling' => 'abort', // This needs to change to redirect
        'handlers' => [
        /**
         * Aborts the execution with a 403 code and allows you to provide the response-text
         */
            'abort' => [
                'code' => 403,
                'message' => 'User does not have any of the necessary access rights.'
            ],

            // Redirect User To Their Respective Home Based On Their Role.
            'redirect' => [
                'url' => '/home',
                'message' => [
                    'key' => 'error',
                    'content' => ''
                ]
            ]
        ]
    ],

主页 URL 因用户类型而异,所以我想知道如何检查以确定用户的类型,然后根据该类型分配他们各自的主页 URL。如果您需要更多代码或更好的解释,请告诉我。

4

0 回答 0