zend-framework2 - 在模型类中使用 ServiceManager 的最佳方式?

标签 zend-framework2

我正在尝试在我的实体类上使用 Service Manager,但我不知道最好的方法。

在 Controller 上这很容易,因为我们可以使用以下命令调用服务管理器: $this->getServiceLocator();

但是,在我的实体类中,即使我实现了 ServiceLocatorAwareInterface 我也可以检索 ServiceManager 因为我的实体类没有被服务管理器调用:

那么最好的方法是什么:

1 - 从我的 Controller 传递我的实体类中的 serviceManager
2 - 使用 ServiceManager 构建我的实体类
3 - ...?

为了更好地理解我的问题,这是我的代码不起作用:

我的实体类:

class Demande extends ArraySerializable implements InputFilterAwareInterface {
/../
    public function getUserTable() {
    if (! $this->userTable) {

        $sm = $this->getServiceLocator();//<== doesn't work !
        $this->userTable = $sm->get ( 'Application\Model\UserTable' );
    }
    return $this->userTable;
}

最佳答案

我不会将 ServiceManager 注入(inject)您的模型(尽管您可以)。我宁愿让 ServiceManager 为您构建模型,并将您需要的任何东西直接注入(inject)模型中。

服务配置:

'factories' => array(
    'SomethingHere' => function($sm) {
        $model= new \My\Model\Something();

        return $model;
    },
    '\My\Model\Demande' => function($sm) {
        $model= new \My\Model\Demande();
        /**
         * Here you use the SM to inject any dependencies you need
         * into your model / service what ever..
         */
        $model->setSomething($sm->get('SomethingHere'));

        return $model;
    },
    /**
     * Alternatively you can provide a class implementing
     * Zend\ServiceManager\FactoryInterface
     * which will provide an instance for you instad of using closures
     */
    '\My\Model\DemandeDefault' => '\My\Model\DemandeFactory',

将您的任何依赖项放在服务管理器配置中,然后使用它为您将任何依赖项注入(inject)您的模型、服务等。

如果您想使用工厂方法而不是闭包,则可以使用示例工厂类:

需求工厂.php
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class DemandeFactory implements FactoryInterface
{
    /**
     * Create a new Instance
     *
     * @param ServiceLocatorInterface $serviceLocator
     * @return Demande
     */
    public function createService(ServiceLocatorInterface $serviceLocator)
    {
        $config = $serviceLocator->get('Config'); // if you need the config..
        // inject dependencies via contrustor
        $model = new \My\Model\Demande($serviceLocator->get('SomethingHere'));
        // or using setter if you wish.
        //$model->setSomething($serviceLocator->get('SomethingHere'));

        return $model;
    }
}

您尝试通过服务管理器实例化的示例模型。

需求.php
class Demande
{
    protected $_something;

    /**
     * You can optionally inject your dependancies via your constructor
     */
    public function __construct($something)
    {
        $this->setSomething($something);
    }

    /**
     * Inject your dependencies via Setters
     */
    public function setSomething($something)
    {
        $this->_something = $something;
    }

    // Something will be injected for you by the Service Manager
    // so there's no need to inject the SM itself.
}

在您的 Controller 中:
public function getDemande() 
{
    if (! $this->_demande) {
        $sm = $this->getServiceLocator();
        $this->_demande = $sm->get ('\My\Model\Demande');
    }
    return $this->_demande;  
}  

您可以将 SergiceManager/ServiceLocator 注入(inject)您的模型,但您的模型将依赖于 ServiceLocator。

关于zend-framework2 - 在模型类中使用 ServiceManager 的最佳方式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14876432/

相关文章:

zend-framework2 - 在哪里可以找到 module.config.php 的所有选项?

navigation - ZF2 将 BjyAuthorize 与 Zend\Navigation 集成

service - Zend Framework 2 覆盖现有服务?

php - 压缩 zend framework 2 的 html 输出

php - Zend Framework 2 - 注释表单和 Doctrine2 - 可捕获的 fatal error - ObjectManager

mysql - Doctrine 2 大写首字母

PHP 代码嗅探器与 Zend Studio 失败

php - 从 ZEND framework 2 Controller 的构造函数重定向

php - Zend Framework 2 - 注释表单、RegEx 验证和自定义错误消息

zend-framework2 - 如何在 Zend Framework 2 中将 db 适配器设置为 Validator RecordExists