繁体   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