Reputation: 363
Specifically, I'm trying to use the FlashMessenger plugin form within my Module.php file.
Right now the method inside my Application/Module.php
file looks like this:
public function checkAcl(MvcEvent $e) {
// code to determine route and role ...
if (!$e->getViewModel()->acl->isAllowed($userRole, $route)) {
$flashMessenger = $e->getController()->plugin('flashMessenger');
$flashMessenger->addMessage('You must be logged in');
// code to redirect to login page ...
}
}
But that is not working because $e->getController() is returning a string, not the controller object. Any help accessing either the controller or the plugin directly is appreciated.
Upvotes: 8
Views: 3562
Reputation: 1273
Alternatively, thus Ezequiel solution works fine for me, you can use the following:
Make sure you use the class in Module.php
use Zend\Mvc\Controller\Plugin\FlashMessenger;
And then:
$flash=new FlashMessenger();
$flash->addErrorMessage("hello");
I'm currently using Ezequiel's one.
Upvotes: 0
Reputation: 7752
You can use the ControllerPluginManager to get an instance of the flashMessenger from any event handler in your Module.php like so:
public function myEventHandler(MvcEvent $e) {
$sm = $e->getApplication()->getServiceManager();
$flash = $sm->get('ControllerPluginManager')->get('flashMessenger');
$flash->addErrorMessage('test');
// ...
}
Obviously you can do this for any controller plugin.
Upvotes: 15