2 回答

TA貢獻1806條經(jīng)驗 獲得超8個贊
最簡單的方法是檢查您應(yīng)該發(fā)回什么樣的響應(yīng):
//In PaymentController.php
public function store()
{
$inputData = $this->validateRequest();
$person = $this->personRepository->findOneByAttribute('id_number', request('id_number'));
if ($person instanceof Person) {
$this->paymentRepository->create($inputData, $person);
if (request()->expectsJson()) {
return response('', 201); // Just a successfully created response
}
return back()->with('successMessage', 'Your payment request registered successfully.');
} else {
if (request()->expectsJson()) {
return response()->json([ 'error' => 'Not found' ], 404); // You can change the error code and message (or remove the message) as needed
}
return back()->with('failureMessage', 'Shoot! Cannot find a person with the given Identification Number.')->withInput();
}
}
Responsible您當(dāng)然可以選擇將其封裝在實現(xiàn)該接口的類中
class PaymentResponse implements Responsible {
private $success;
public function __construct($success) {
$this->success = $success;
}
public function toResponse($request) {
if ($this->success) {
if (request()->expectsJson()) {
return response()->json([ 'error' => 'Not found' ], 404); // You can change the error code and message (or remove the message) as needed
}
return back()->with('failureMessage', 'Shoot! Cannot find a person with the given Identification Number.')->withInput();
}
if (request()->expectsJson()) {
return response()->json([ 'error' => 'Not found' ], 404); // You can change the error code and message (or remove the message) as needed
}
return back()->with('failureMessage', 'Shoot! Cannot find a person with the given Identification Number.')->withInput();
}
}
那么你的代碼將是:
//In PaymentController.php
public function store()
{
$inputData = $this->validateRequest();
$person = $this->personRepository->findOneByAttribute('id_number', request('id_number'));
if ($person instanceof Person) {
$this->paymentRepository->create($inputData, $person);
return new PaymentResponse(true);
} else {
return new PaymentResponse(false);
}
}
當(dāng)然,您也可以將控制器邏輯提取到單獨的庫中,然后擁有兩個單獨的控制器方法,并且如果需要,仍然可以使用負責(zé)的對象。這實際上取決于您的用例以及最適合您的方法

TA貢獻1906條經(jīng)驗 獲得超3個贊
我認為另一種方法是利用服務(wù)存儲庫模式。您將應(yīng)用程序服務(wù)包裝在一個單獨的類中。服務(wù)是控制器和存儲庫之間的交互器。流程看起來像[request] -> [controller] -> [service] -> [repository]。
通過利用此模式,您可以在應(yīng)用程序的不同區(qū)域重復(fù)使用您的服務(wù)。例如,您可以擁有一個專門用于服務(wù)傳統(tǒng) Web 應(yīng)用程序的控制器,以及一個通過返回 JSON 數(shù)據(jù)來服務(wù) SPA 的控制器,但服務(wù)相同的業(yè)務(wù)流程。
例如:
付款回復(fù):
class PaymentStoreResponse
{
protected $message;
protected $code;
protected $extraData;
public function __construct($message, $code, $extraData = "")
{
$this->message = $message;
$this->code = $code;
$this->extraData = $extraData;
}
public function getMessage()
{
return $this->message;
}
public function getCode()
{
return $this->code;
}
public function getExtraData()
{
return $this->extraData;
}
}
付款服務(wù):
function store($data)
{
$person = $this->personRepository->findOneByAttribute('id_number', $data('id_number'));
if ($person instanceof Person) {
$this->paymentRepository->create($inputData, $person);
return new PaymentResponse("paymentSuccess", 201, "successMessage");
} else {
return new PaymentResponse("notFound", 404, "failureMessage");
}
}
控制器:
// App\Controllers\Web\PaymentController.php
function store(Request $request)
{
$inputData = $this->validateRequest();
$response = $this->paymentService->store($inputData);
return back()->with($response->getExtraData(), $response->getMessage())
}
// App\Controllers\Api\PaymentController.php
function store(Request $request)
{
// validation might be different because
// api might need to authenticate with jwt etc.
$inputData = $this->validateRequest();
$response = $this->paymentService->store($inputData);
return response()->json(['message' => $response->getMessage()], $response->getCode());
}
這將產(chǎn)生一個更干凈的控制器,因為控制器將只處理請求驗證和響應(yīng),同時將業(yè)務(wù)流程委托給服務(wù)類(支付服務(wù))。業(yè)務(wù)邏輯也集中在服務(wù)層,這意味著如果業(yè)務(wù)發(fā)生變化,它將應(yīng)用到API控制器和Web控制器。因此,它將把你從重構(gòu)噩夢中拯救出來。
您可以探索不同的架構(gòu),例如 Clean Architecture + DDD。它將使您的開發(fā)體驗更好,通過依賴抽象實現(xiàn)領(lǐng)域邏輯的集中化和層與層之間的低耦合。
- 2 回答
- 0 關(guān)注
- 153 瀏覽
添加回答
舉報