我有问题从URL获取参数到我的Yii 2控制器或模型,通过WebService发送后请求。
例如,URL:https://example.com/?keyword=test&id=1234
我的模型保存功能代码是:
public function save($keyword)
{
$httpClient = new Client();
$data = [
'civilite' => $this->civility,
'nom' => $this->lastName,
'prenom' => $this->firstName,
'telephone' => $this->phoneNumber,
'email' => $this->emailAddress,
'operateur' => $this->operator,
'tel_domicile' => $this->phone,
'keyword' => $keyword,
];
$preferences = explode(',', $this->preferences);
$index = 0;
foreach ($preferences as $preference) {
$index++;
$data['attente' . $index] = $preference;
}
LeadLogHelper::log($data);
$rawResponse = $httpClient->createRequest()
->setMethod('POST')
->setUrl(\Yii::$app->params['WebserviceUrl'])
->setData($data)
->send();
$response = json_decode($rawResponse->content);
if (!$response->Statut) {
Yii::error('An error occurred while saving the data using the webservice', __METHOD__);
Yii::error($data, __METHOD__);
Yii::error($response, __METHOD__);
}
return $response->Statut == 1 || $response->Message === 'Already exist.';
}
我的控制器Sumbit操作是:
public function actionSubmit()
{
$leadModel = new LeadModel();
$data = LeadModelFormHelper::transformDataFormToModel(Yii::$app->request->post());
$leadModel->setAttributes($data);
if (!$leadModel->validate()) {
return $this->sendValidationErrorResponse($leadModel->getErrors());
}
$bestOffer = $leadModel->getBestOffer();
$isSuccessfullySaved = $leadModel->save(Yii::$app->request->get('keyword'));
if (!$isSuccessfullySaved) {
$response = $this->asJson(['errors' => ['webservice' => 'Error while saving the lead']]);
$response->statusCode = 550;
return $response->send();
}
$lastStep = Json::decode(file_get_contents(__DIR__ . '/../config-offers/offers.json'));
$isSuccessfullySent = Yii::$app->mailer->compose(
'offer-summary',
ArrayHelper::merge($leadModel->getAttributes(), $lastStep['offres'][$bestOffer])
)
->setFrom(['noreply@choisirmabox.fr' => 'CHOISIR MA BOX'])
->setTo($leadModel->emailAddress)
->setSubject('choisirmabox.fr - Votre offre')
->send();
if (!$isSuccessfullySent) {
Yii::error("Could not send the email", __METHOD__);
}
return $this->asJson(['offer' => $bestOffer]);
}
关键字有一个空值,有人能帮助我吗?!!
1条答案
按热度按时间kmbjn2e31#
【更新】
当我在过去遇到这个问题时,我通过打印所有的get参数(如
\Yii::$app->request->getQueryParams()
或\Yii::$app->request->get()
)来确保我访问了正确的参数,只是为了确保get参数被看到,也可能是如果在到达actionSubmit()之前通过另一个操作,GET参数丢失了。通过打印\Yii::$app->request
来检查referer可以帮助快速看到之前的内容。