如何在create()
中处理许多重复参数。我喜欢看起来整洁,可读。
例如,在控制器中,我们有addList
()方法,其中包含许多参数。
public function addList(CreateListRequest $request)
{
$created = $this->list->create(
$request->user(),
$request->name,
$request->subject,
$request->description,
$request->location,
$request->do_email,
$request->provider,
$request->something1,
$request->something2,
);
}
在List
类中,我们有一个创建方法,也有许多重复的许多参数。并且在$this->api->create()
和$this->listRepository->create()
中也有点重复。有没有办法清理它或重构它?
class List
{
public function create($user, $name, $subject, $description, $location, $doEmail, $provider, $something, $something2)
{
$list = $this->api->create($name, $subject, $description, $location);
if ($list->status == "success") {
// Add to database
$row = $this->listRepository->create($user->id, $name, $subject, $description, $location, $doEmail, $provider, $something, $something2);
return $row;
}
return false;
}
}
答案 0 :(得分:0)
传递请求对象是好的,而不是发送每个 参数单独。
public function addList(CreateListRequest $request) {
$created = $this->list->create($request);
}
在List类中,由于请求对象无论如何都有用户会话, 只需将请求对象传递给listRepository类。
class List {
public function create($request) {
$list = $this->api->create($request->only(['name', 'subject', 'description', 'location']));
if ($list->status == "success") {
$row = $this->listRepository->create($request);
return true;
}
return false;
}
}