Jump to content

MVC question


mika

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/265311-mvc-question/
Share on other sites

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');
  }
.
.
.
.
}

Link to comment
https://forums.phpfreaks.com/topic/265311-mvc-question/#findComment-1359680
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.