Get the service locator in anywhere in zf2 statically (bad practice but needed sometimes :-) )

dev-master 2015-01-14 20:41 UTC

This package is auto-updated.

Last update: 2023-04-06 11:54:46 UTC


This is a long term fork of the fezfez/ServiceLocatorFactory as that is no longer maintained...

Allow you to get ServiceManager from everywhere in your application by calling this static factory. Yes its probably very bad practise to do it this way and not use complete DI, however sometimes you just gotta do what you gotta do.

Can be installed with composer - see


namespace Corp\News;

use Corp\News\NewsDAO;
use Zend\ServiceManager\ServiceManager;
use Corp\ServiceLocator\ServiceLocator;

class NewsDAOFactory
    private function __construct()

     * @return \Corp\News\NewsDAO
    public static function getInstance()
        $sm = ServiceLocatorFactory::getInstance();
        $em = $sm->get('doctrine.entitymanager.orm_default');

        return new NewsDAO($em);