从 Url 获取参数到 Yii2 模型

Get Parametrs from Url to Yii2 model

我在从 URL 获取参数到我的 Yii2 模型时遇到问题。

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

我的代码是:

public function save()
    {
        $httpClient = new Client();
        $keyword = Yii::$app->getRequest()->getQueryParam('key');
        $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['leadWebserviceUrl'])
            ->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 === 'La Fiche existe déjà.';

    }

保存功能有效,但 $Keyword 的值为空,请帮助!!

取决于您的查询 link 例如:https://example.com/?key=test&id=1234

当你调用 $model->save() 方法时,对于这个特定的模型你可以传递一个额外的参数作为 $key 像这样:

方法一

//action controller
//Your model
 $model->save(\Yii::$app->request->get('key'))

这是模型:

public function save($key = '')
    {
        $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' => $key, // Or use if statement to include this value or not
        ];
        ...
    }

但是像这样使用模型属性是安全的:

方法二

//define a property
class YOUR_MODEL extends Model {
    ...
    public $key;
    ...
    public function rules()
    {
        return [
            ...
            [['key'], 'safe'],
            ...
        ];
    }
}

然后你可以在控制器中使用它:

$model->key = \Yii::$app->request->get('key');

在您的模型中进行更改:

public function save()
    {
        $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' => $this->key
        ];

        ...
    }

然后调用 $model->save() 方法。

希望这对您有所帮助。