一般来说,我有一个我在yii2中编写的api,我决定摆脱不必要的代码,如果在检查过程中出现错误则抛出异常,我这样做:
if ($ticket->tickets_left > $count_ticket) {
$ticket->tickets_left -= $count_ticket;
$hours = json_decode($ticket->schedule_minute);
foreach ($hours as $hour) {
if ($hour == (int)$order['hours']) {
if ($hour->tickets_left > $count_ticket) {
if ((int)$order['ticketsCount']['adults']) {
$total_price += ((int)$order['ticketsCount']['adults'] * $hour->price_adults);
}
if ((int)$order['ticketsCount']['students']) {
$total_price += ((int)$order['ticketsCount']['students'] * $hour->price_stud);
}
if ((int)$order['ticketsCount']['children']) {
$total_price += ((int)$order['ticketsCount']['kid'] * $hour->price_children);
}
$hour->tickets_left -= $count_ticket;
foreach ($hour->minutes as $minute) {
if ($minute->minute == (int)$order['minutes']) {
if ($minute->count_ticket_left > $count_ticket) {
$minute->count_ticket_left -= $count_ticket;
} else {
return throw new UnprocessableEntityHttpException(Yii::t('app', 'Sorry, tickets for the selected time have run out'));
}
}
}
} else {
return throw new UnprocessableEntityHttpException(Yii::t('app', 'Sorry, tickets for the selected time have run out'));
}
}
}
} else {
return throw new UnprocessableEntityHttpException(Yii::t('app', 'Sorry, tickets for the selected time have run out'));
}
就是这样,一切都应该工作,但这是我通过邮递员得到的回应:
ParseError: syntax error, unexpected 'throw' (T_THROW), expecting ';' in
D:\OpenServer\domains\iBrush\teleportAdmin\api\controllers\OrderController.php:90
Stack trace:
#0 [internal function]: yii\BaseYii::autoload()
#1 [internal function]: spl_autoload_call()
#2 D:\OpenServer\domains\iBrush\teleportAdmin\vendor\yiisoft\yii2\base\Module.php(643): class_exists()
#3 D:\OpenServer\domains\iBrush\teleportAdmin\vendor\yiisoft\yii2\base\Module.php(602): yii\base\Module->createControllerByID()
#4 D:\OpenServer\domains\iBrush\teleportAdmin\vendor\yiisoft\yii2\base\Module.php(594): yii\base\Module->createController()
#5 D:\OpenServer\domains\iBrush\teleportAdmin\vendor\yiisoft\yii2\base\Module.php(528): yii\base\Module->createController()
#6 D:\OpenServer\domains\iBrush\teleportAdmin\vendor\yiisoft\yii2\web\Application.php(104): yii\base\Module->runAction()
#7 D:\OpenServer\domains\iBrush\teleportAdmin\vendor\yiisoft\yii2\base\Application.php(392): yii\web\Application->handleRequest()
#8 D:\OpenServer\domains\iBrush\teleportAdmin\web\index.php(12): yii\base\Application->run()
#9 {main}
基本上,我需要你的帮助。
总的来说,一切都简单地决定了,我们删除了 throw 的返回,并在网络上我们这样写: