简体   繁体   中英

Use component from listener in CakePHP 4

I'm trying to fire a flash from a listener, the listener fires correctly but the component keeps failing.

$flash = new FlashComponent(new ComponentRegistry(new \Cake\Controller\Controller()));

$message = 'message';
$options = [
   'params' => [
       'title' => 'title',
       'type' => 'warning'
   ]
];

$flash->modal($message, $options);

Is this even possible?

Event Listeners don't have access to controller components. But you can get the current request object from the router via

$request = Router::getRequest();

Then you can get the FlashMessage Utility object via

$flash = $request->getFlash();

And finally call the flash messages you are used to from the controller via

$flash->info('Some info message');

And the flash messages should appear as you expect them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM