Laravel 5 - 在控制器文件中为多个路由定义中间件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28645163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:59:27  来源:igfitidea点击:

Laravel 5 - Defining middleware for multiple routes in controller file

phplaravelcontrollerviewsmiddleware

提问by Talky

Stackers! I'm currently learning laravel5 and I love it, but I'm struggling with one thing. Since Laravel 5 we have Middleware which we can use in controller's construct function, like this:

堆垛机!我目前正在学习 laravel5 并且我喜欢它,但我正在为一件事而苦苦挣扎。从 Laravel 5 开始,我们有了可以在控制器的构造函数中使用的中间件,如下所示:

Controller file:

控制器文件:

public function __construct()
{
    $this->middleware('admin', ['only' => 'create']);
}

Now what I want is to define HERE^ (not in routes file) middleware to be used in multiple views, like 'create', 'edit' and 'show'. defining

现在我想要的是定义在多个视图中使用的 HERE^(不在路由文件中)中间件,例如“创建”、“编辑”和“显示”。定义

public function __construct()
{
            $this->middleware('admin', ['only' => 'create|edit|show']);
}

Unfortunately does not work. I'd rather not use routes. Any ideas, dear friends?

不幸的是不起作用。我宁愿不使用路线。有什么想法吗,亲爱的朋友?

回答by lukasgeiter

Simply pass an array instead of a string with |delimiter:

只需传递一个数组而不是带有|分隔符的字符串:

public function __construct()
{
    $this->middleware('admin', ['only' => ['create', 'edit', 'show']]);
}