Zend Framework 2:无法在自定义类上获得servicelocator

时间:2017-01-03 05:04:06

标签: zend-framework2

我无法在自定义类上加载服务locatior,请参阅下面的错误代码,任何建议都会被评估

namespace Application\Helper;



use Zend\Mail\Message;
use Zend\Mail\Transport\Smtp as SmtpTransport;
use Zend\Mime\Message as MimeMessage;
use Zend\Mime\Part as MimePart;
use Zend\Mail\Transport\SmtpOptions;
use Zend\Mail\Transport\Sendmail;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class EmailHelper implements  ServiceLocatorAwareInterface{

    protected $serviceLocator;


    public function testEmail($email){

        $config = $this->serviceLocator->get('Config');
        print_r($config); exit;


    }

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

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

}
  

在null中调用成员函数get()   /var/www/html/engsvc_dev/module/Application/src/Application/Helper/EmailHelper.php   在第23行

1 个答案:

答案 0 :(得分:3)

为了注入ServiceManager实例Zend\ServiceManager\ServiceLocatorInterface,你需要实例化这样的类:

$emailHelper = $this->getServiceLocator()->get('EmailHelper');

另外不要忘记在Module.php中注册该课程

public function getServiceConfig()
{
    return array(
        'invokables' => array(
            'EmailHelper' => 'Application\Helper\EmailHelper'
        )
    );
}