【发布时间】:2021-12-06 12:18:31
【问题描述】:
上下文
我已经定义了一个服务类 validatePanel,它在实际呈现 CRUD 面板之前被调用。此类的目的是确保一系列不同的 CRUD 一个接一个地完成。 DB NbSuivis 中的一个简单计数器跟踪面板之间的进程。该类只是重定向到序列中的适当面板。
这是类定义:
public static function validatePanel(int $NbPanel, int $NbSuivis, int $id): void
// $NbPanel = Panel number to be validated
// $NbSuivis = Last panel number which has been saved
// $id = id of model
{
switch ($NbPanel) {
case 2: {
switch ($NbSuivis) {
case -1:
\Alert::error('Exposition en suivi externe')->flash();
redirect()->route('exposition/identification.edit', ['id' => $id])->send();
break;
case 0:
\Alert::error('Veuillez compléter l\'onglet d\'identification')->flash();
redirect()->route('exposition/identification.edit', ['id' => $id])->send();
break;
default:
break;
}
}
case 3:
switch ($NbSuivis) {
case -1:
\Alert::error('Exposition en suivi externe')->flash();
redirect()->route('exposition/identification.edit', ['id' => $id])->send();
break;
case 0:
\Alert::error('Veuillez compléter l\'onglet d\'identification')->flash();
redirect()->route('exposition/identification.edit', ['id' => $id])->send();
break;
case 1:
\Alert::error('Veuillez compléter l\'onglet nature de l\'exposition')->flash();
redirect()->route('exposition/declaration.edit', ['id' => $id])->send();
break;
default:
break;
}
default:
break;
}
}
CRUD 面板中的一个示例调用是:
namespace App\Http\Controllers\Admin;
use App\Services\ProcessService;
...
CRUD::setValidation(DeclarationRequest::class);
$id = $this->crud->getCurrentEntry()->id;
$NbSuivis = $this->crud->getCurrentEntry()->NbSuivis;
ProcessService::validatePanel(2, $NbSuivis, $id);
问题
这会产生重定向过多
解决方法
但是,当我将 switch 语句直接嵌入到各种 CRUD 控制器中时,一切正常。
我怎样才能将这个逻辑排除在控制器之外和类定义中?
【问题讨论】: