我在 Laravel 應用程序中有一個數組,我想在 Laravel 偵聽器中修改它。PHP 默認按值傳遞一個數組,但是,Laravel 事件及其偵聽器的工作方式我無法修改原始變量。有沒有比我在下面做的更好的方法?從中觸發事件的模型。型號: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; }}監聽器: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. }}
通過引用 Laravel 事件偵聽器傳遞數組
慕無忌1623718
2021-07-09 16:01:08