K
K
kot-airplane2017-08-28 11:59:12
PHP
kot-airplane, 2017-08-28 11:59:12

How to pass the result of the Model to the View?

Method in controller:

public function createUser()
    {
        $model = new UserModel();
        $result_create = $model->createUser($POST = '');

        $view = new afterCreateView();
    
    // если удачно то
        $result = $view->succsessUserCreate($result_create);
    
    // если неудачно то
        $result = $view->failUserCreate($result_create);
    
        return $result;
    }

The model validates the data from the POST and, if everything is in order, creates the user. In what form is it better to return data from the model to the controller so that it would be convenient to call different views depending on the result? So far, the only idea that comes to my mind is with an array of the form:
$res = array(
  'error' => 1,
  'error_msg' => 'Такой e-mail уже есть в БД'
);

Those. The control checks if we call the success view, if not, then the failure view. You can put an error message not in this array, but in the session. $res['error'] === 0

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2017-08-28
@webinar

You create the $errors variable in the model and store all the errors in the array there, and the method adds the error there. However, this method only returns true or false. Accordingly, the action will be something like this:

if($model->someMethod()){
 return $view->succsessUserCreate($result_create);
}
return $view->failUserCreate($result_create,$model->errors);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question