Belicoff
Belicoff

Reputation: 69

GET parameters from URL Yii2

I have issue with getting parameters from URL to my Yii2 Controller or Model, to send it in post request via WebService.

Ex. URL : https://example.com/?keyword=test&id=1234

My Model Save Function code is :

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.';

    }

My Controller Sumbit Action is :

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(['[email protected]' => '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]);
}

The keyword have a NULL value, can someone help me please ?!!

Upvotes: 0

Views: 2439

Answers (1)

hmg
hmg

Reputation: 186

[Update]

When I've encountered this issue in the past, I make sure I'm accessing the right params by printing all get params like \Yii::$app->request->getQueryParams() or \Yii::$app->request->get() just to make sure get params are seen, could be also that the GET parameters were lost if passes thought another action before getting to actionSubmit(). Checking the referer by printing \Yii::$app->request could help quickly seeing where is coming before.

Upvotes: 1

Related Questions