Laravel 5 中的钩子?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36226021/
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
Hooks in Laravel 5?
提问by Warz
I'm creating a package and want hook functionality (the package should inject some extra validation rules when a user updates a field in my app).
我正在创建一个包并想要挂钩功能(当用户更新我的应用程序中的字段时,该包应该注入一些额外的验证规则)。
I managed to do this using the event system. What I do is pass the $rules variable and $request into the listener, I modify the $rules variable and return it.
我设法使用事件系统做到了这一点。我所做的是将 $rules 变量和 $request 传递给侦听器,我修改 $rules 变量并返回它。
Would this be bad practice? What would be the recommended way of doing it?
这会是不好的做法吗?推荐的做法是什么?
I mean, it works. I'm just unsure if this is the best way to go about it.
我的意思是,它有效。我只是不确定这是否是最好的方法。
Code below:
代码如下:
SettingsController.php (this is under App/ and where I'm validating on update)
SettingsController.php(这是在 App/ 下,我在那里验证更新)
public function update(Setting $setting, Request $request)
{
$rules = [
'package' => 'required|in:'.implode(config('app.packages'),','),
'name' => 'required|max:255|alpha_dash|not_contains:-|unique:auth_setting,name,'.$setting->id.',id,package,'.$setting->package,
'description' => '',
];
// Is this bad??
$rules = Event::fire(new SettingsWereSubmitted($request,$rules))[0];
$v = Validator::make($request->all(),$rules);
Then in my package (packages/exchange/src/Listeners) I got this listener (ValidateSettings.php):
然后在我的包 (packages/exchange/src/Listeners) 中我得到了这个监听器 (ValidateSettings.php):
public function handle(SettingsWereSubmitted $event)
{
if($event->request->package == 'exchange')
{
// Add rules
$rules = [
'fee' => 'required|decimal|min_amount:0|max_amount:1|max_decimal:8',
'freeze_trade' => 'required|in:1,0',
];
$event->rules['value'] = $rules[$event->request->name];
return $event->rules;
}
}
回答by Andrey Degtyaruk
I'm looking at this piece of your code
我在看你的这段代码
if($event->request->package == 'exchange')
and think that you can achieve the same behaviour easier by using required_if validation rule.
并认为您可以通过使用 required_if 验证规则更轻松地实现相同的行为。
$rules = [
'package' => 'required|in:'.implode(config('app.packages'),','),
'name' => 'required|max:255|alpha_dash|not_contains:-|unique:auth_setting,name,'.$setting->id.',id,package,'.$setting->package,
'description' => '',
'fee' => 'required_if:package,exchange|decimal|min_amount:0|max_amount:1|max_decimal:8',
'freeze_trade' => 'required_if:package,exchange|in:1,0',
];
ADDED: By the way, I would suggest using Request classes to validate income requests and remove validation code from controllers because validation of request is responsibility of Request but not Controller. It's pretty easy in Laravel. First, you create your request class in your Http\Requests folder:
补充:顺便说一句,我建议使用请求类来验证收入请求并从控制器中删除验证代码,因为请求的验证是请求的责任,而不是控制器的责任。在 Laravel 中这很容易。首先,在 Http\Requests 文件夹中创建请求类:
class UpdateSomethingRequest extends Requst
{
public function rules()
{
return [
'package' => 'required|in:'.implode(config('app.packages'),','),
'name' => 'required|max:255|alpha_dash|not_contains:-|unique:auth_setting,name,'.$setting->id.',id,package,'.$setting->package,
'description' => '',
'fee' => 'required_if:package,exchange|decimal|min_amount:0|max_amount:1|max_decimal:8',
'freeze_trade' => 'required_if:package,exchange|in:1,0',
];
}
}
And then just remove that code from you Controller and type-hint new request class to update method like following:
然后只需从您的控制器中删除该代码并键入提示新的请求类以更新方法,如下所示:
public function update(Setting $setting, UpdateSomethingRequest $request)
{
// Your request is already validated here so no need to do validation again
}