gpt4 book ai didi

zend-framework - ZF3 : SharedEventManager injection in EventManager

转载 作者:行者123 更新时间:2023-12-02 09:08:50 25 4
gpt4 key购买 nike

在 ZF2 中,使用共享事件管理器在多个 Controller 之间共享事件

模块.php

use Zend\Mvc\MvcEvent;

class Module
{
public function getConfig()
{
return include __DIR__ . '/../config/module.config.php';
}

public function onBootstrap(MvcEvent $event)
{
$services = $event->getApplication()->getServiceManager();
$sharedEventManager = $eventManager->getSharedManager();

$sharedEventManager->attach('user', 'log-fail', function($event) use ($services) {
$username = $event->getParam('username');
$log = $services->get('log');
$log->warn('Error logging user: ' . $username);
});
}
}

LogController.php

<?php

namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\EventManager\EventManager;

class LogController extends AbstractActionController
{
public function __construct()
{
}

public function inAction()
{
//...
if (! $isValid) {
$event = new EventManager('user');
$event->trigger('log-fail', $this, array('username'=> $username));
//...
}
}
}

但是 ZF3 要求在实例化时注入(inject)共享事件管理器,而不是通过 setter。 https://docs.zendframework.com/zend-eventmanager/migration/changed/

所以我创建了一个日志 Controller 工厂来注入(inject)共享事件管理器,但我找不到如何进行注入(inject)

LogControllerFactory.php

<?php

namespace Application\Controller;

use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Factory\FactoryInterface;

class LogControllerFactory implements FactoryInterface
{
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
$sharedEventManager = ???

return new LogController($sharedEventManager);
}
}

您是否知道如何实例化共享事件管理器以将其注入(inject) Controller 中,以便将其放入 Controller __construct 函数中?

最佳答案

回答我自己的问题:

<强>1。配置.php

配置文件

/..
'controllers' => [
'factories' => [
Controller\LogController::class => Controller\LogControllerFactory::class,
/..
],
'aliases' => [
'log' => Controller\LogController::class,
/..
]
],
/..
'service_manager' => [
'factories' => [
'log' => Service\Log::class,
],
],
/..

<强>2。 Module.php

模块文件

namespace Application;

use Zend\Mvc\MvcEvent;

class Module
{
public function getConfig()
{
return include __DIR__ . '/../config/module.config.php';
}

public function onBootstrap(MvcEvent $event)
{
// Get shared service manager
$services = $event->getApplication()->getServiceManager();
$sharedEventManager = $eventManager->getSharedManager();

// Attach listener
$sharedEventManager->attach('user', 'log-fail', function($event) use ($services) {
$username = $event->getParam('username');
$log = $services->get('log');
$log->warn('Error logging user: ' . $username);
});
}
}

<强>3。 LogService.php

服务文件

namespace Application\Service;

use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Factory\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\Log\Logger;
use Zend\Log\Writer\Stream as StreamWriter;
use Zend\Log\Filter\Priority as PriorityFilter;

class Log implements FactoryInterface
{
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
$controllerPluginManager = $container;
$serviceManager = $controllerPluginManager->get('ServiceManager');

// Instantiate Zend\Log\Logger
$log = new Logger();
// Set stream to write in a log file
$writer = new StreamWriter('data/logs/audit.log');
$log->addWriter($writer);

return $log;
}
}

<强>4。 LogControllerFactory.php*

Controller 工厂文件

namespace Application\Controller;

use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Factory\FactoryInterface;

class LogControllerFactory implements FactoryInterface
{
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
$controllerPluginManager = $container;
$eventManager = $controllerPluginManager->get('EventManager');
$sharedEventManager = $eventManager->getSharedManager();

return new LogController($sharedEventManager);
}
}

<强>5。 LogController.php

Controller 文件

namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\EventManager\EventManager;
use Zend\EventManager\SharedEventManager;

class LogController extends AbstractActionController
{
public function __construct(SharedEventManager $sharedEventManager)
{
$this->_sharedEventManager = $sharedEventManager;
}

public function inAction()
{
//...
if (! $isValid) {
$event = new EventManager($this->_sharedEventManager, ['user']);
$event->trigger('log-fail', $this, array('username'=> $username));
//...
}
}
}

关于zend-framework - ZF3 : SharedEventManager injection in EventManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54949679/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com