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.

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.

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