当前位置: 动力学知识库 > 问答 > 编程问答 >

php - Right way to return from model to controller

问题描述:

I am sorry for asking this type of question but I'm getting really frustrated by this situation

This is my controller

public function changeName_PUT() {

header('Content-Type: application/json');

$return = \Models\User::getCurrent()->changeName(Input::data());

return json_encode($return);

}

And this is my model

public function changeName($data){

$rules = array( ... );

$messages = array( ... );

$valid = Input::validate($data, $rules, $messages);

if($valid === true){

return $this->putIntoDb(array('name' => $data['name'])) ? 'Namechanged successfully.' : 'An error occurred.';

}else{

return $valid->getMessage();

}

}

My problem is that no matter whether the model succeeds or fails there is always a string returned, so in the controller I cannot determine if the name was changed or not. If I return only true or false then in the controller, if something failed, I wouldn't know what it was. My other option is to return arrays from the model like array('error', 'message') but that looks so ugly and gives me the feeling that I'm doing it wrong. Can you point me the right way?

网友答案:

You have different approaches :

  • Simple version : If you insert, return the inserted_id|false. If you delete, return true|false. If you update, return true|false. If you select, return array|string|false.

  • Class version : You return true or false (except for select which returns the queried string|array). And you store in a class variable the error messages so that you can do something like :

    $success = $model->query('blabla');
    if (!$success)
        print_r($model->getMessages());
    
  • Object version : Same as the simple version, except that you return the database object on a successful select or update query (else false).

  • Exception version : Same as object version, but instead of returning false, you throw an error that describes the problem.

Now, if you look at common frameworks you will see that CodeIgniter uses the object version, Phalcon uses the class version, Zend uses the exception version. There is no "best-way", just use a method and stick with it for all your models.

(Just do not return a string that explains the error on a failed query!)

网友答案:

Model layer should not be sending data to the controllers at all.

In the MVC architectural pattern the controllers are responsible for altering the state of model layer. And that's it. View instances then pull what information they need from said model layer.

If, because of controller's alterations, an error state is triggered in the model layer, then controller does not care about it. It is only important to the view, which, based on whether an error happened, might pick a different set of templates for assembly of response.

P.S.: the HTTP headers are part of response. So, why are your "controllers" generating a response? It's basically no different from putting an echo there.

分享给朋友:
您可能感兴趣的文章:
随机阅读: