我在 Laravel 應(yīng)用程序中有一個(gè)數(shù)組,我想在 Laravel 偵聽(tīng)器中修改它。PHP 默認(rèn)按值傳遞一個(gè)數(shù)組,但是,Laravel 事件及其偵聽(tīng)器的工作方式我無(wú)法修改原始變量。有沒(méi)有比我在下面做的更好的方法?從中觸發(fā)事件的模型。型號(hào):Event.phpnamespace Vendor\Package\Modelsuse Vendor\Package\Events\PageNodeArrayAfter;use Event;class Page{ public function toArray() { $data = []; // do something with the data. Event::fire(new PageNodeToArrayAfter($data)) // The data should be modified by a listener when I use it here. }}事件:PageNodeToArrayAfter.phpnamespace Vendor\Package\Events;class PageNodeToArrayAfter{ /** * Props to be sent to the view * @var array $data */ protected $data = []; /** * @param array $data * */ public function __construct(array &$data) { $this->data = $data; } public function getData() { return $this->data; }}監(jiān)聽(tīng)器:FlashMessagesListner.phpnamespace Vendor\Package\Listeners;class FlashMessagesListner{ protected $data = []; public function handle(PageNodeToArrayAfter $event) { $this->data = $event->getData(); // The problem here is the $data is no logner a reference here. }}
通過(guò)引用 Laravel 事件偵聽(tīng)器傳遞數(shù)組
慕無(wú)忌1623718
2021-07-09 16:01:08