Hi everyone. I saw a lot of topics with such titles but none of them solved my problem.
Case. I have a controller with index action:
class TrackController extends ControllerBase {
public function indexAction(){
$this->eventsManager->fire('track:beforeStart', $this);
// Some other logic
}
}
Event "track" binded in dispatcher:
// ...
$evManager->attach('track', new Track);
// ...
And the Track listener:
class Track extends Generic
{
/**
* @param Event $event
*/
public function beforeStart(Event $event)
{
if($someCondition == true)
{
$this->dispatcher->forward(
[
'controller' => 'forwardController',
'action' => 'forwardAction'
]
);
}
}
}
I want to dispatch if($someCondition == true)
forward, send response and then stop dispatching. Now it doesn't work, after forwardAction
it returns to TrackController::indexAction
and continue work after fire
.
I've tried to remove all events manually via $this->eventsManager->detachAll()
, tried to run dispatch
and send response manually but I can't make it work as I wish. The only way is to call exit
, but this is a bad way and it's really hard to test.
Please help with advice. Is it possible to end dispatching within event or may be another (better) solution exists. I know I can do it this way:
class TrackController extends ControllerBase
{
public function beforeExecuteRoute()
{
if($someCondition == true)
{
$this->dispatcher->forward(
[
'controller' => 'forwardController',
'action' => 'forwardAction'
]
);
} else {
// run another method
}
}
}
But don't want to copypaste it in some other controllers. I thought the event-listener model is the best option.
Thanks in advance.