簡體   English   中英

Laravel 5.3:本地主機重定向您太​​多次

[英]Laravel 5.3: localhost redirected you too many times

我有2個用戶角色,分別是superadminadmin

我不希望admin訪問“設置頁面”。

我不確定這是否是正確的方法。


所以,這是我的SettingsController.php

class SettingsController extends Controller {
    public function index() {
        if(Auth::user()->roles == 0) {
            return redirect(url()->previous());
        } else {
            return view('settings.index');
        }
    }
}

如您所見, roles是否為0。我將用戶重定向到他們所在的最后一頁。我還嘗試使用return back()


web.php(路由)

<?php

Route::get('/', ['uses' => 'UsersController@index']);
Route::post('login', ['uses' => 'UsersController@login']);

Route::group(['middleware' => ['auth']], function() {
    Route::get('logout', ['uses' => 'UsersController@destroy']);
    Route::get('upline', ['uses' => 'UplinesController@index']);
    Route::get('upline/create', ['uses' => 'UplinesController@create']);
    Route::post('upline', ['uses' => 'UplinesController@store']);
    Route::delete('upline/destroy/{id}', ['uses' => 'UplinesController@destroy']);
    Route::put('upline/update/{id}', ['uses' => 'UplinesController@update']);
    Route::get('upline/getdownlines/{id}', ['uses' => 'UplinesController@getDownlines']);

    Route::get('downline', ['uses' => 'DownlinesController@index']);
    Route::post('downline', ['uses' => 'DownlinesController@store']);
    Route::delete('upline/destroy/{id}', ['uses' => 'DownlinesController@destroy']);
    Route::put('downline/update/{id}', ['uses' => 'DownlinesController@update']);

    Route::get('bonus', ['uses' => 'BonusController@index']);
    Route::post('bonus/csv', ['uses' => 'BonusController@fileUpload']);

    Route::get('settings', ['uses' => 'SettingsController@index']);
});

我有第二個問題。 我可以使用中間件限制管理員嗎? 如果是,怎么辦?

任何幫助,將不勝感激。

也許是第二個選項,“ 使用中間件限制管理員 ”。 因此,您可以嘗試類似的方法;

Route::group(['prefix' => 'admin', 'middleware' => 'auth'], function () {
    Route::get('/', 'DownlinesController@update');
});

然后

Route::group(['prefix' => 'super', 'middleware' => 'auth'], function () {
    Route::get('/', 'UplinesController@index');
});

正如@michael的答案建議使用中間件一樣,他的答案也無法說明如何實現(我也是,我只是添加了更多文字)。

注意 :Laravel很大,因為它提供了文檔,請使用它

您有2個(或更多選擇):

  • 參數化中間件
  • 2種獨特的中間件(一個用於管理員,另一個用於超級管理員)

注意 :使用artisan從存根生成中間件, $ php artisan make:middleware MyNewShinyMiddleware

參數化中間件(我的選擇)

頭文件,並檢查了這個

示例完全顯示了您的問題。

public function handle($request, Closure $next, $role)
{
    if (! $request->user()->hasRole($role)) { //implement hasRole in User Model
        // Redirect... 
        // (use named routes to redirect or do 401 (unauthorized) because thats what is going on!
        // abort(401) // create view in /views/errors/401.blade.php
        // return redirect()->route('home');
    }

    //success user has role $role, do nothing here just go to another "onion" layer
    return $next($request);
}

2種獨特的中間件

只需創建兩個中間件,然后對角色的檢查例程進行硬編碼(與您在控制器示例中所做的相同),只是使用$request->user()


(路由)web.php

Route::group(['middleware' => 'role:admin'], function () {...} //parametrized

Route::group(['middleware' => 'checkRoleAdmin'], function () {...}
Route::group(['middleware' => 'checkRoleSuper'], function () {...}

注意rolecheckRoleAdmincheckRoleSuper是“命名”中間件,您需要在kernel.php中注冊它們


另一種方法是您使用最合適的閘門或策略,因為您正試圖限制用戶。 在這里閱讀更多。

我將基於中間件的ACL用於非常簡單的項目(例如一名管理員,沒有實際用戶)。
我將基於Gates的ACL用於中等項目(1-2個角色)。
我將基於策略的ACL用於“龐大”項目(許多角色,許多用戶)。

還可以考慮查看https://github.com/Zizaco/entrust

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM