Hi all,

I have a question concerning the MVC logic. In my example I have to add a 
new user to a mail server via REST. And I would like to inform the user 
with the error message. So should the Model return the error message to the 
controller or should the controller request the error message from the 
model? What is best practice?

1) Model send message to controller
Model:
class MailAddress extends AppModel {

    public function addMailAddress( $mail, $password) {
        // create POST data
        $arrData = array('Password' => $password);
        
        // perform the request
        $result = $this->REST->request('POST', '/users/create/name/'.$mail, 
$arrData);
        // check the results
        if($result === false) {
            $this->lastError = $this->REST->lastError;
            return false;
        } elseif ( $result['api']['userCreate']['status'] == 'success' ) {
            return true;
        } else {
            $this->lastError = 
$result['api']['userCreate']['response']['message'];
            return false;
        }
    }
}


Controller:
if($this->EmailAddress->addMailAddress($mail,$password)) {
    $this->Session->setFlash( __('Entry successfully saved.') );
} else {
    $this->Session->setFlash( __('Error on adding: %s', 
$this->EmailAddress->lastError) );
}

2) Controller gets message from the model

Model:
class MailAddress extends AppModel {

    public function addMailAddress( $mail, $password) {
        // create POST data
        $arrData = array('Password' => $password);
        
        // perform the request
        $result = $this->REST->request('POST', '/users/create/name/'.$mail, 
$arrData);
        // check the results
        if($result === false) {
            return new ErrorObject($this->REST->lastError);
        } elseif ( $result['api']['userCreate']['status'] == 'success' ) {
            return true;
        } else {
            return new 
ErrorObject($result['api']['userCreate']['response']['message']);
        }
    }
}
Controller:
if(!($ret = $this->EmailAddress->addMailAddress($mail,$password)) 
instanceof ErrorObject) {
    $this->Session->setFlash( __('Entry successfully saved.') );
} else {
    $this->Session->setFlash( __('Error on adding: %s', $ret->getMessage()) 
);
}


3) I don't want to use Exceptions here because i think returning values 
keeps the programming flow more straight.

Thanks, Tobi

-- 
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

--- 
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
Visit this group at http://groups.google.com/group/cake-php?hl=en.


  • MVC Tobias Schlemmer

Reply via email to