将其他参数添加到事件订阅者

时间:2016-08-02 22:55:00

标签: symfony events listener

我正在使用symfony 3.我已经创建了一些事件并且它们工作正常。但这个新事件是不同的。我必须以某种方式向订户发送一些额外的参数。最好的方法是什么?我创建了phenstalk服务来执行工作。控制器发送如下事件:

$dispatcher = $this->container->get('event_dispatcher');
    $dispatcher->dispatch(Events::POWERPLANT_GET_DATA, new     PowerPlantEvent($this->getPowerPlantAction($id)));

这样的调用不起作用,因为我错过了一些参数。

我在订阅者中采取了行动:

public function onPowerPlantGetData(PowerPlantEvent $event, $startDate, $endDate, $measurement, $field)

怎么做?

2 个答案:

答案 0 :(得分:0)

您应该将所有必要的数据放入事件本身,然后您可以在订阅者

中访问它们

为简单起见,我公开了属性,并将所有属性添加到__construct,但是由您决定如何实现它。您可能希望为该

创建新的Event类
class PowerPlantEvent extends Event
{
    /**
     * @var \DateTime
     */
    public $startDate;

    /**
     * @var \DateTime
     */
    public $endDate;

    /**
     * @var Something
     */
    public $measurement;

    public $field;

    /**
     * PowerPlantEvent constructor.
     *
     * @param \DateTime $startDate
     * @param \DateTime $endDate
     * @param Something $measurement
     * @param           $field
     */
    public function __construct(\DateTime $startDate, \DateTime $endDate, Something $measurement, $field)
    {
        $this->startDate = $startDate;
        $this->endDate = $endDate;
        $this->measurement = $measurement;
        $this->field = $field;
    }
}

并用

调用它
$dispatcher = $this->container->get('event_dispatcher');
$dispatcher->dispatch(
    Events::POWERPLANT_GET_DATA,
    new PowerPlantEvent($startDate, $endDate, $measurement, $field)
);

并在订阅者(或事件监听者)中进行访问

public function onPowerPlantGetData(PowerPlantEvent $event)
{
    $startDate = $event->startDate; // you might want to implement getters for those
}

答案 1 :(得分:0)

如果您使用可选参数,则可以在构造函数中将它们设为可选

use Symfony\Component\EventDispatcher\Event;

final class PowerPlantEvent extends Event
{
    /**
     * @var PowerPlantAction
     */
    private $powerPlantAction;

    /**
     * @var \DateTime
     */
    private $startDate;

    /**
     * @var \DateTime
     */
    private $endDate;

    /**
     * @var int
     */
    private $measurement;

    /**
     * @var string
     */
    private $field;

    public function __construct(
        $powerPlantAction,
        $startDate = null,
        $endDate = null,
        $measurement = null,
        $field = null
    ) {
        $this->powerPlantAction = $powerPlantAction;
        $this->startDate = $startDate;
        $this->endDate = $endDate;
        $this->measurement = $measurement;
        $this->field = $field;
    }

    public function getPowerPlantAction()
    {
        return $this->powerPlantAction;
    }

    public function getStartDate()
    {
        return $this->startDate;
    }

    public function getEndDate()
    {
        return $this->endDate;
    }

    public function getMeasurement()
    {
        return $this->measurement;
    }

    public function getField()
    {
        return $this->field;
    }
}

然后像这样调度:

$this->eventDispatcher->dispatch(
    Events::POWERPLANT_GET_DATA,
    new PowerPlantEvent($this->getPowerPlantAction($id))
);

或者:

$this->eventDispatcher->dispatch(
    Events::POWERPLANT_GET_DATA,
    new PowerPlantEvent(
       $this->getPowerPlantAction($id),
       $startDate,
       $endDate,
       $measurement,
       $field
    )
);

在您的订阅者中,只需使用这些获取者:

public function onPowerPlantGetData(PowerPlantEvent $powerPlantEvent)
{
    $powerPlantEvent->getStartDate();
    // ...
}
相关问题