What is the right way to handle $_POST data in MVC?

前端 未结 5 1107
时光取名叫无心
时光取名叫无心 2020-11-28 06:10

I have the common MVC situation in my PHP system: the Controller receive a request from the View containing $_POST data. Now I have th

5条回答
  •  死守一世寂寞
    2020-11-28 06:13

    Look at some MVC frameworks.

    For example, in Yii you can write such code inside action:

    $model = new Model();
    if(isset($_POST['Model'])) {
        $model->attributes = $_POST['Model'];
    }
    

    Note, that all attributes of your model must be passed through validation rules. In Yii validation applies during (actually, before) $model->save()

    See:

    1. http://www.yiiframework.com/doc/guide/1.1/en/form.model#securing-attribute-assignments
    2. http://www.yiiframework.com/doc/guide/1.1/en/basics.mvc

提交回复
热议问题