Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

How can I access controller objects from models?

How can I access current controller objects from models?

Lets say, i have a bumch of public properties on the current controller and i want to access those properties on some function of my model. how can i access those from model.



84.9k
Accepted
answer

class myModel extends Model {

    function a() {
        $this->getDi()->getShared("dispatcher")->getgetActiveController();
    }
}

// i havent tested it, but it sohuld be something like that

The model should have no knowledge of the controller. If you want to pass values from the controller to the model, you should push them from the controller, not pull them from the model.

Why do you need to access controller properties from a model? Perhaps there's a better way?

The model should have no knowledge of the controller. If you want to pass values from the controller to the model, you should push them from the controller, not pull them from the model.

Why do you need to access controller properties from a model? Perhaps there's a better way?

Hi @Dylan thanks for your suggestion. Well, I had to add some minor feature on the old app and it have huge code base. And i didn't want to take risk to break down the application. I will certanly follow your concept when i get enough tim. : )