How to get container(sm) in model's static function?

I import a third lib to zf3 model folder, it has some static public function inside, and inside some static function, it use some configuration which i configed in module range configuration file global.php. that means i should get $container(so called sm locator) inside the static function which use via aclass::staticFunc() kind way. I don’t know how. anybody can help?

That does not mean you need the service locator, only a single service - in your case a part of the configuration.

You should do the following:

  • create a factory for your class (there you can fetch the config and add to you class)
  • register your class with factory as service
  • fetch the class from the service-manager and use it

Never inject the entire service locator, only single service(s).

I don’t think it woks. It means I need to do a lot of modification to the third-part library.

The library class use many static method inside.

Why do you think it would work if you add the entire service-locator?


I’m sorry, but I don’t know your class and how it looks like. At the moment it is hard to help here. I can only guess and speculate.

Hi there, thanks for your efforts!

As you said, if registered as single service of the third-part libary [for example, named xxxclass], it seems no problem. When I use a static method of this class, we named it yyymethod, it is a static method, the document of this library told me to use it like xxxclass::yyymethod(). in the yyymethod, I need to get a global configration, may be a array.

I don’t think I can get some registered service from a static method which used like xxxclass::yyymethod(), this seems xxxclass::yyymethod() have no connection with the outside world, it is a independent logic against the outside.

Are there any descriptions or a documentation how the configuration should be set for this library? Or is the third library a secret?


Please do not let me ask for everything and become more concrete. Otherwise I can only answer you in general.
Yes, I can give an answer like “use the bootstrap in the module class”, but I don’t know if this answer is correct or good.
Help me and I can help you. I’m not a magician or wizard or god. I can’t see your code and I don’t even know what your application is meant to do.

not a secret, lol

In fact, it is one of my works. Open-source code, But my php level is very low, that’s why I didn’t say it out.

Very simple and ugly orginazed code. :blush:

It is not the final version I used in my project. The version I used I didnot pushed to github.

I pasted a little piece as follow as the class:

static public function randomBgImg($container, $action = ‘get’)
{
$config = $container->get(‘config’);
$bgImgArr = $config[‘ImageDragAuth’][‘bgimg’];
$cookieDomain = $config[‘cookieDomain’];

    if ($action == 'set' || !isset($container->get('Request')->getCookie()->imgDragBgRand))
    {
        $indexRand = array_rand($bgImgArr);
        $cookieImgDragBgRand = $indexRand;
        $container->get('Response')->getHeaders()->addHeader(new SetCookie('imgDragBgRand',$indexRand,time()+600,'/',$cookieDomain));
    }
    
    if (!isset($cookieImgDragBgRand)) $cookieImgDragBgRand = $container->get('Request')->getCookie()->imgDragBgRand;
    return isset($bgImgArr[$cookieImgDragBgRand]) ? $bgImgArr[$cookieImgDragBgRand] : $bgImgArr[$indexRand];
}

I integrate into my zf3 project with pass a $contianer to this static method.

And I got the $contianer via controller’s contrcution fucntion like:
class ImagedragauthController extends AbstractActionController
{
private $container;

public function __construct(ServiceManager $container)
{
    $this->container = $container;
}

I configed LazyControllerAbstractFactory inside my module configuration.

The problem seems solved, however it is not a better way to there.
It is what it looks like https://www.wendangs.com/api/imagedragauth/demo