简体   繁体   English

Laravel控制器如何实现多防护访问方法?

[英]How to Laravel controller multi guard access methods?

I am trying to call middle-ware in constructor of my controller. 我正在尝试在控制器的构造函数中调用中间件。

My PostController class is below 我的PostController类在下面

class PostController extends Controller
{
    public function __construct()
    {
        $this->middleware( ['auth:admin', ['only'=> ['store', 'update']]], ['auth:client', ['only'=> ['index', 'view']]]);
    }    
}

Please suggestion or correct me if I am wrong. 如果我错了,请提出建议或纠正我。

I think the best way do it in routes 我认为最好的方法是在路线上

Route::post('path', 'IndexController@store')->middleware(['auth:admin']);
Route::get('path', 'IndexController@index')->middleware(['auth:client]);

Or in group, for example: 或分组,例如:

Route::group(['middleware' => ['auth:admin']], function ($route) {
   $route->post('storePath', 'IndexController@store');
   $route->put('updatePath', 'IndexController@update');
});

Yes, you can call the middleware function multiple times. 是的,您可以多次调用中间件函数。

class PostController extends Controller
{

    public function __construct()
    {
        $this->middleware('auth:admin', ['only'=> ['store', 'update']])
        $this->middleware('auth:client', ['only'=> ['index', 'view']]);
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM