Zend FrameWork 2 Get ServiceLocator In Form and populate a drop down list

前端 未结 6 1343
没有蜡笔的小新
没有蜡笔的小新 2021-02-02 14:40

I need to get the adapter from the form, but still could not.

In my controller I can recover the adapter using the following:



        
6条回答
  •  不思量自难忘°
    2021-02-02 15:34

    An alternative method to the other answers would be to create a ServiceManager Initializer.

    An example of an existing Initializer is how the ServiceManager is injected if your instance implements ServiceLocatorAwareInterface.

    The idea would be to create an interface that you check for in your Initialiser, this interface may look like:

    interface FormServiceAwareInterface
    {
        public function init();
        public function setServiceManager(ServiceManager $serviceManager);
    }
    

    An example of what your Initializer may look like:

    class FormInitializer implements InitializerInterface
    {
        public function initialize($instance, ServiceLocatorInterface $serviceLocator)
        {
            if (!$instance instanceof FormServiceAwareInterface)
            {
                return;
            }
    
            $instance->setServiceManager($serviceLocator);
            $instance->init();
        }
    }
    

    Anything that happens in init() would have access to the ServiceManager. Of course you would need to add your initializer to your SM configuration.

    It is not perfect but it works fine for my needs and can also be applied to any Fieldsets pulled from the ServiceManager.

提交回复
热议问题