Jump to content

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

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.

Link to comment
https://forums.phpfreaks.com/topic/265311-mvc-question/#findComment-1359659
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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.