I'm using Laminas framework (laminas/mvc v3.1) / Zend3. In certain cases I need to call a different action than prescribed for a given route, i.e. display a default page to users who are not authorised to view restricted content.
To do this, I've tweaked the onDispatch method in Module.php as follows:
public function onDispatch(MvcEvent $event) {
// check if content is restricted to the current user
if (content-is-restricted) {
$event->getRouteMatch()->setParam("controller", "{namespace}\Controller\{anotherController}");
$event->getRouteMatch()->setParam("action", "{defaultAction}");
$event->getRouteMatch()->setMatchedRouteName("{defaultRoute}");
}
else {
proceed-as-normal-to-the-action-prescribed-by-route
}
}
Unfortunately, the snippet above renders the following error:
A 404 error occurred
Page not found.
The requested controller was unable to dispatch the request.
Controller:
{namespace}\Controller\{anotherController}
No Exception available
This works though if the action is within the same controller but not if it's in a different one. What am I missing? Note, I don't want to redirect the user and change the URL - just substitute what is showed to them