0

How do I use the Zend Framework Service Locator in a Model? I have a class that I would like to use the a Table Gateway Model in. I have followed the Album example and would like to access the table outside of the controller. However if I copy and paste the code from the controller into the class I need it I get an error (undefined method :getServiceLocator()). How do I use this 'class' outside of the controller?

In the end I would like to access the functions in the " class AlbumTable" in something other then the controller (in this case another class). Thanks.

class Calendar implements ServiceLocatorAwareInterface{ 

    protected $serviceLocator;

public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
{
    $this->serviceLocator = $serviceLocator;
}

public function getServiceLocator()
{
    return $this->serviceLocator;
}
/*
 * Create Calendar Sync Table
 */
 public function getCalendarSyncTable()
 {
     if (!$this->calendarSyncTable) {
         $sm = $this->getServiceLocator();
         $this->calendarSyncTable = $sm->get('Pro\Model\CalendarSync\CalendarSyncTable');
     }
     return $this->calendarSyncTable;  
 }   

Needed to change how I called it in the controller to

$calendar = $this->getServiceLocator()>get('Pro\Model\GoogleCalendar\Calendar');
Matt
  • 383
  • 1
  • 5
  • 20
  • [Possible duplicate](http://stackoverflow.com/questions/12770966/service-locator-in-zend-framework-2) – AlexP Mar 24 '15 at 12:39

1 Answers1

4

If you want to use ServiceLocator in any class, just implement ServiceLocatorAwareInterface. For example:

class SomeClass implements ServiceLocatorAwareInterface
{
    protected $serviceLocator;

    public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
    {
        $this->serviceLocator = $serviceLocator;
    }

    public function getServiceLocator()
    {
        return $this->serviceLocator;
    }

ZendFramework2 will automaticaly inject instance of ServiceLocator to your class. Read more about ServiceManager here

Rodin Vasiliy
  • 573
  • 4
  • 7
  • I'm getting a null value when I var dump service manager. I have posted my code above. Is there an additional step I am missing? – Matt Mar 24 '15 at 14:22
  • How do you call `Calendar`? If you write just `new Calendar()`, then yes, `$this->serviceLocator` will be null. You need to add your `Calendar` to ServiceManager config, like `'invokables' => array('Calendar' => 'Application\Service\Calendar')`, then this magic would work. Check [this answer](http://stackoverflow.com/a/12793595/1129939) for more details – Rodin Vasiliy Mar 24 '15 at 14:56
  • Does it go in this array? To this point I have only used the module.conig for routing and controller. 'controllers' => array( 'invokables' => array( 'Pro\Model\GoogleCalendar\Calendar' => 'Pro\Model\GoogleCalendar\Calendar') – Matt Mar 24 '15 at 15:03
  • In side this array? 'service_manager' => array( 'abstract_factories' => array( 'Pro\Model\GoogleCalendar\Calendar' => 'Pro\Model\GoogleCalendar\Calendar)) – Matt Mar 24 '15 at 15:14
  • Yes, you should use `service_manager` key. Check examples [here](http://framework.zend.com/manual/current/en/modules/zend.service-manager.quick-start.html#examples) – Rodin Vasiliy Mar 24 '15 at 15:20
  • I feel like I am close but still getting null. I have tried setting up as you did above and also tried the below (how it was configured in another stack overflow post) 'service_manager' => array( 'invokables' => array( 'Pro\Model\GoogleCalendar\Calendar' => 'Pro\Model\GoogleCalendar\Calendar', ), ), – Matt Mar 24 '15 at 15:27
  • Can you add example of using `Calendar` class to the first post? Do you call it from controller, or another class? I think the problem is in the way you use it – Rodin Vasiliy Mar 24 '15 at 15:53
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/73675/discussion-between-matt-and-rodin-vasiliy). – Matt Mar 24 '15 at 15:56
  • I call it in the controller $calendar = new Calendar(); – Matt Mar 24 '15 at 16:03
  • Final decision after discussion: not to use `new Calendar()`, use `$this->getServiceLocator()->get('Pro\Model\GoogleCalendar\Calendar')` (works in controllers) – Rodin Vasiliy Mar 24 '15 at 16:29