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

Access to controller in model?

Hi guys! Just me again :)

in controller:

$locale = $this->request->getBestLanguages();

How it retyping in model before create function?

public function beforeCreate()
{
    $this->locale = .... // need getBestLanguages
}


260
Accepted
answer
edited Nov '15

How about to use Phalcon\Registry?

Firstly you should register Phalcon\Registry service in DI (in index.php for example):

$di->set('registry', function()  {
    return new Phalcon\Registry();
}, TRUE);

Controller:

$this->registry->locale = $this->request->getBestLanguages(); 

Model:

public function beforeCreate()
{
    $this->locale = $this->getDI()->getShared('registry')->locale
}

Or you can just pass a variable in the model constructor.

Controller

$model = new Model($this->request->getBestLanguages();

Model:


    public function __construct($locale = "en")
    {
        $this->locale = $locale;
    }

Just pass it to model when creating it, by constructor, or when update/create method. Or by registry/session.

спасибо Андрей, то что надо.