2016-08-02 28 views
1

我正在使用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) 

如何做到這一點?

+0

我可以dispach事件,如:$調度 - > dispatch(Events :: POWERPLANT_GET_DATA,new Powe rPlantEvent($ this-> getPowerPlantAction($ id)),$ startDate,$ endDate,$ measurement,$ field);或者有其他方法嗎? – sanof

回答

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(); 
    // ... 
} 
+0

Tnx,我試過了,它工作正常。這部分事件的文檔很差。我也考慮創建新課程或爲此命令。因爲有了這個,我開始使用influx數據庫從另一個symfony應用程序獲取數據thrue phenastalk作業。事件是實現這個功能的最好方式,還是使用自定義類或symfony命令會更好? – sanof

+0

好。我沒有什麼信息可以給你提供有用的建議。我會嘗試兩種方法,並保持一個更適合你的方法。我個人更喜歡事件。命令(在命令總線的術語中)將是更大的體系結構更改,這裏沒有附加值。 –

0

你應該把所有必要的數據到事件本身,然後你可以訪問這些用戶

爲了簡單起見我做公共的屬性,並添加所有__construct,但如何實現它,它是給你的。你可能希望爲

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 
} 
相關問題