Php 在ZF2类中获取配置的简单方法
我想创建一个简单的类,它将向给定的电子邮件地址发送预定义的电子邮件。因此,我创建了以下类:Php 在ZF2类中获取配置的简单方法,php,zend-framework2,Php,Zend Framework2,我想创建一个简单的类,它将向给定的电子邮件地址发送预定义的电子邮件。因此,我创建了以下类: namespace Custom; use Zend\Mail\Message; use Zend\Mail\Transport\Smtp as SmtpTransport; use Zend\Mail\Transport\SmtpOptions; class Notification { public function sendEmailNotification($to) {
namespace Custom;
use Zend\Mail\Message;
use Zend\Mail\Transport\Smtp as SmtpTransport;
use Zend\Mail\Transport\SmtpOptions;
class Notification
{
public function sendEmailNotification($to)
{
try {
$message = new Message();
$message->addTo($to)
->addFrom('test@example.com')
->setSubject('Hello')
->setBody('Predefined email body');
$transport = new SmtpTransport();
$options = new SmtpOptions(array(
'name' => 'smtp.example.com',
'host' => 'smtp.example.com',
'port' => '587',
'connection_class' => 'plain',
'connection_config' => array(
'username' => 'test@example.com',
'password' => 'somepasswd',
'ssl' => 'tls',
),
));
$transport->setOptions($options);
$transport->send($message);
} catch (\Exception $e) {
echo $e->getMessage();
}
}
}
然后,从控制器发送电子邮件,其中包括:
$notification = new \Custom\Notification();
$notification->sendEmailNotification('example@example.com');
这正是我们想要的
我想做的下一件事是将邮件服务器配置参数移动到项目配置文件(local.php
)中。问题是-如何获取\Custom\Notification类(不是控制器)中的配置参数
对于像我这样的初学者来说,我找到的解决方案似乎太复杂了。执行类似于$config=$this->getServiceLocator()->get('config')代码>你必须在整个项目中施展魔法
是否有一种从自定义类中的配置文件获取数据的简单方法 你必须使用注射来达到你的目的。只需为控制器创建一个工厂,将配置注入控制器
namespace Application\Controller\Service;
class YourControllerFactory
{
public function __invoke(ContainerInterface $container)
{
$serviceLocator = $container->getServiceLocator();
$config = $serviceLocator->get('config');
$controller = new YourController($config);
return $controller;
}
}
为此,控制器需要一个构造函数,该构造函数将配置作为参数
namespace Application\Controller;
class YourController extends AbstractActionController
{
protected $config;
public function __construct($config)
{
$this->config = $config;
}
public function indexAction()
{
// inject your notification class with the config
$notification = new \Custom\Notification($this->config);
$notification->sendEmailNotification('example@example.com');
}
}
因此,您的通知类需要一个将config作为参数的构造函数
namespace Application\Controller;
class YourController extends AbstractActionController
{
protected $config;
public function __construct($config)
{
$this->config = $config;
}
public function indexAction()
{
// inject your notification class with the config
$notification = new \Custom\Notification($this->config);
$notification->sendEmailNotification('example@example.com');
}
}
其他办法
解决问题的另一种方法是将通知类注册为服务。只需为通知类创建一个工厂,在其中创建所有需要的内容,然后将其注入通知类
namespace Application\Mail;
class Notification
{
protected $config;
public function __construct($config)
{
$this->config = $config;
}
public function sendEmailNotification($to)
{
}
}
工厂本身非常简单,因为我们在控制器工厂中看到了几乎相同的方法
namespace Application\Mail\Service;
class NotificationFactory
{
public function __invoke(ContainerInterface $container)
{
$config = $container->get('config');
$notification = new Notification($config);
return $notification;
}
}
现在您只需在服务管理器部分的module.config.php
文件中记录它
'service_manager' => [
'factories' => [
Notification::class => NotificationFactory::class,
],
],
从现在起,您可以使用zend framework 2的服务容器访问通知类。还记得上面显示的控制器实例的工厂吗?与其将配置注入控制器,不如将通知本身注入控制器。您的通知类将通过通知工厂使用所需的配置创建
namespace Application\Controller\Service;
class YourControllerFactory
{
public function __invoke(ContainerInterface $container)
{
$serviceLocator = $container->getServiceLocator();
$notification = $serviceLocator->get(Notification::class);
return new YourController($notification);
}
}
玩得开心 你必须使用注射来达到你的目的。只需为控制器创建一个工厂,将配置注入控制器
namespace Application\Controller\Service;
class YourControllerFactory
{
public function __invoke(ContainerInterface $container)
{
$serviceLocator = $container->getServiceLocator();
$config = $serviceLocator->get('config');
$controller = new YourController($config);
return $controller;
}
}
为此,控制器需要一个构造函数,该构造函数将配置作为参数
namespace Application\Controller;
class YourController extends AbstractActionController
{
protected $config;
public function __construct($config)
{
$this->config = $config;
}
public function indexAction()
{
// inject your notification class with the config
$notification = new \Custom\Notification($this->config);
$notification->sendEmailNotification('example@example.com');
}
}
因此,您的通知类需要一个将config作为参数的构造函数
namespace Application\Controller;
class YourController extends AbstractActionController
{
protected $config;
public function __construct($config)
{
$this->config = $config;
}
public function indexAction()
{
// inject your notification class with the config
$notification = new \Custom\Notification($this->config);
$notification->sendEmailNotification('example@example.com');
}
}
其他办法
解决问题的另一种方法是将通知类注册为服务。只需为通知类创建一个工厂,在其中创建所有需要的内容,然后将其注入通知类
namespace Application\Mail;
class Notification
{
protected $config;
public function __construct($config)
{
$this->config = $config;
}
public function sendEmailNotification($to)
{
}
}
工厂本身非常简单,因为我们在控制器工厂中看到了几乎相同的方法
namespace Application\Mail\Service;
class NotificationFactory
{
public function __invoke(ContainerInterface $container)
{
$config = $container->get('config');
$notification = new Notification($config);
return $notification;
}
}
现在您只需在服务管理器部分的module.config.php
文件中记录它
'service_manager' => [
'factories' => [
Notification::class => NotificationFactory::class,
],
],
从现在起,您可以使用zend framework 2的服务容器访问通知类。还记得上面显示的控制器实例的工厂吗?与其将配置注入控制器,不如将通知本身注入控制器。您的通知类将通过通知工厂使用所需的配置创建
namespace Application\Controller\Service;
class YourControllerFactory
{
public function __invoke(ContainerInterface $container)
{
$serviceLocator = $container->getServiceLocator();
$notification = $serviceLocator->get(Notification::class);
return new YourController($notification);
}
}
玩得开心