We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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.



85.5k
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. : )