mika Posted July 6, 2012 Share Posted July 6, 2012 Hi I'm trying to implement MVC design in my personal framework and I was wondering if it is a good idea to pass the controller object to the model... M, V and C are supposed to be independent but I'd like to be able to call the controller when something unexpected happens inside the model. For example, when validating query parameters the model should be able to notify the controller what went wrong and a controller action should be triggered. What is the best way to accomplish that? thanks Quote Link to comment https://forums.phpfreaks.com/topic/265311-mvc-question/ Share on other sites More sharing options...
trq Posted July 6, 2012 Share Posted July 6, 2012 No it's not a good idea to pass a controller to a Model. For example, when validating query parameters the model should be able to notify the controller what went wrong and a controller action should be triggered. Generally you call Models from the controller. I don't see your issue. Quote Link to comment https://forums.phpfreaks.com/topic/265311-mvc-question/#findComment-1359659 Share on other sites More sharing options...
mika Posted July 6, 2012 Author Share Posted July 6, 2012 My issue: this is a simplified piece of code to demonstrate what I'm trying to do. I need some kind of interaction, a way to instruct the controller what to do when a model parameter is not valid. I'd like to have an automatic validation(before query execution) that the controller is not aware about, a method like $myModel->validate(); inside the controller seems redundant. class Controller_User extends Controller { public function __construct() { parent::__construct(); } public function msg($text) { parent::display(array('message'=>$text)); } . . . } class Model_User extends User { protected $_controller; public function __construct($controller) { $this->_controller = $controller; } public function onParameterNotValid($name) { /* here you can have a call to user redirect, a retype of the wrong value, ... */ $this->_controller->msg('Parameter "' . $name . '" is not valid'); } . . . . } Quote Link to comment https://forums.phpfreaks.com/topic/265311-mvc-question/#findComment-1359680 Share on other sites More sharing options...
ignace Posted July 7, 2012 Share Posted July 7, 2012 Something like this? $validator = $model->getValidator(); if (!$validator->isValid()) { $this->display(array('messages' => $validator->getMessages())); } Quote Link to comment https://forums.phpfreaks.com/topic/265311-mvc-question/#findComment-1359850 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.