duanmo5724 2015-05-26 14:51
浏览 20
已采纳

ZF2 - 如何在我的模型中获取应用程序配置项?

In my job I am dealing with a legacy app running on ZF2. There is a model which is sending out a variety of different emails to difference addresses. The one thing they have in common is they all need to BCC to one particular address.

At first, in my head I was cursing the previous developer, for foolishly hard coding the email address 20 different times in one file. I assumed that it would be a piece of cake to grab an application config with a simple call $this->config->get('x') (like in Laravel) or something along them lines. Now I find myself feeling bad, because I understand why the previous dev did hard code the email addresses.

So to the question, how the hell do I grab a config item from application.config.php inside the model? I keep reading about how I need to implement the ServiceLocaterAware Interface. Is this really necessary? There must be a way to grab configs easily, surely?!?

  • 写回答

2条回答 默认 最新

  • doumindang2416 2015-05-26 15:34
    关注

    How the hell do I grab a config item from application.config.php inside the model?

    You shouldn't do so inside, do it 'outside'.

    Register your model class as a service in module.config.php.

    'service_manager' => [
        'factories' => [
            'Email\Model\EmailModel' => 'Email\Model\EmailModelFactory',
        ]
    ],
    

    Then create the factory Email\Model\EmailModelFactory, this uses the service manager to fetch the 'email' config key and injects it into the model's constructor.

    namespace Email\Model;
    
    use Email\Model\EmailModel;
    use Zend\ServiceManager\ServiceLocatorInterface;
    use Zend\ServiceManager\FactoryInterface;
    
    class EmailModelFactory implements FactoryInterface
    {
        public function createService(ServiceLocatorInterface $serviceLocator)
        {
            return new EmailModel($this->getEmailOptions($serviceLocator));
        }
    
        // Return the email options
        public function getEmailOptions(ServiceLocatorInterface $serviceLocator)
        {
            $options = $serviceLocator->get('config');
            return $options['email'];
        }
    }
    

    The issue you will now have is all calls to your model classes will have to use $serviceManager->get('Email\Model\EmailModel') (rather than new \Email\Model\EmailModel) in order for the configuration to be injected. Even without seeing any of your legacy application my guess is that this would be difficult.

    The model should not be responsible for sending emails; you could replace it with an service class, e.g. 'EmailService' and repeat the injection example above just for this class.

    EmailService::send(EmailModel $email, array $options);
    

    This would keep your model independent and there would be no need to replace the calls to new Model etc.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同