Service locator in Zend Framework 2

前端 未结 3 2031
我寻月下人不归
我寻月下人不归 2020-12-28 08:52

In controller I create and use my model so

public function getAlbumTable()
{
    if (!$this->albumTable) {
                


        
3条回答
  •  青春惊慌失措
    2020-12-28 09:33

    Zend MVC will inject the ServiceLocator instance into a class implementing Zend\ServiceManager\ServiceLocatorAwareInterface. A simple implementation for a model table looks like the following:

    serviceLocator = $serviceLocator;
      }
    
      public function getServiceLocator() {
        return $this->serviceLocator;
      }
    
      // now instance of Service Locator is ready to use
      public function someMethod() {
        $table = $this->serviceLocator->get('Album\Model\AlbumTable');
        //...
      }
    }
    

提交回复
热议问题