简体   繁体   中英

ZF2 view helper in model

I'm localizing project written on ZF2. And in some places guys use static variables in Model layer.

    public function getTransitions()
    {
        $transitions = array(
            static::STATE_NEEDS_REVIEW         => 'Needs Review',
            static::STATE_NEEDS_REVISION       => 'Needs Revision',
            static::STATE_ARCHIVED             => 'Archive'
        );
//      some code...
        return $transitions;
    }

I have custom view helper for translation which is basically wrapper of ZF2 translator with some additional functionality. I know how to use it in view layer and in Controllers like this:

$t = $this->getServiceLocator()->get('ViewHelperManager')->get('t');

But how can use my view helper in Model layer ???

If you need to access the service locator in the model, you can implement ServiceLocatorAwareInterface, as described in the question and answer here:

injecting ServiceLocator via ServiceLocatorAwareInterface doesnt work

However, I would suggest that using a view helper in the model might not be the right way to go. Perhaps you could take the relevant logic from the ViewHelper and move it to a service that you can make available to the model class either via injection or just creating it when needed. The view helper could then use the same service.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM