0
次のコードとロジックについてバディと議論しています。 POSTリクエストを処理するシステムがあります(RESTではなく、リクエストを投稿するだけです)。考えを知るには、次のPHPを参照してください。KISSとメソッドのヘックロード
class Pancake {
public function servePancake()
{
if (/* check something on the kitchen*/) {
echo json_encode(array('status' => 'error', 'message' => 'kitchen offline'));
exit;
}
if (/* check something else on the kitchen */) {
echo json_encode(array('status' => 'error', 'message' => 'Santa hates you, no pancakes this time'));
exit;
}
if (/* check if there's something else in the menu */) {
echo json_encode(array(
'status' => 'weDoHaveMenuYouShouldCheckItOut',
'message' => 'See the menu for a pancake flavor you wish',
'pancakeTypes' => array('cherry', 'blueberry', 'blackberry')
));
exit;
}
// And so on with lot's of options, but pretty simple inside
// if everything went fine
echo json_encode(array('status' => 'ok', 'message' => 'Here is your pancake'));
exit;
}
}
回答ごとにメソッドを作成する理由はありますか?
protected function respondWithMenu($message, $menu)
{
// basically the same json_encode and exit;
}
protected function respondWithSuccess($message);
{
// Status is succes + same json_encode
}
protected function respondWithError($message)
{
// Status is error + same json_encode
}
protected function respondWithSomethingElse($message, $somethingElse)
{
// adding something else to the response
// and then.... gues what?
// yeah, json_encode, you're correct!
}
直接json_encodeコールの代わりに使用してください。
ありがとうございました。