在Yii中使用循环获取all _POST变量

hs1rzwqc  于 2022-11-09  发布在  其他
关注(0)|答案(3)|浏览(99)

我有密码

$str = '';
foreach ($_POST as $k => $v) {
    $str .= $k.'='.$v;
}

是否可以使用CHttpRequest进行相同操作?未找到任何方法。
需要将md5它放在最后,这样就没有安全问题了。

vh0rcniy

vh0rcniy1#

我不认为CHttpRequest是用来获取post数据的,Yii本身使用了$_POST变量,例如Gii的默认输出可以是:

public function actionCreate()
    {
        $model=new Model;

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);

        if(isset($_POST['Model'])) //<- POST data var
        {
            $model->attributes=$_POST['Model']; //<- POST data var
            if($model->save())
                $this->redirect(array('view','id'=>$model->id));
        }

        $this->render('create',array(
            'model'=>$model,
        ));
    }
omvjsjqw

omvjsjqw2#

You can extendCHttpRequestcomponent and rewrite public methodgetPost()

class HttpRequest extends CHttpRequest {
    public function getPost($name = null, $defaultValue = null) {
        $data = ($this->_isJsonEncoded()) ? \CJSON::decode($this-
            >getRawBody()) : $_POST;

        if (isset($data[$name])) {
            return $data[$name];
        } elseif (isset($defaultValue)) {
            return $defaultValue;
        } else {
            return $data;
        }
    }
}

Do not forget to include the new component class inconfig/main.phpfile:

'request' => [
     'class' => 'application.components.HttpRequest',
 ],

Now, you can get all**$_POST**data in this way:

Yii::app()->request->getPost();
wfauudbj

wfauudbj3#

您可以使用getRestParams()来获取数组。

Yii::app()->request->getRestParams()

Yii::$app->request->getRestParams()

**引用:**CHttpRequest -获取剩余参数

相关问题