I have a code that executes before page is rendered in Module.php
like this.
$eventManager->attach(MvcEvent::EVENT_RENDER, function(MvcEvent $event) {
/** @var ServiceManager $sm */
$sm = $event->getParam('application')->getServiceManager();
$logger = $sm->get(LogService::class)->getLogger();
$themeResolver = $sm->get(ThemeResolveService::class);
$df = $sm->get(DataFetchService::class);
$params = $sm->get('ControllerPluginManager')->get('params');
$security = $sm->get(SecurityService::class);
try {
$cleanedParams = $security->clean($params->fromRoute());
} catch (\Exception $e) {
echo $e->getMessage();
$logger->info($e->getMessage());
die();
}
/** theme resolving code **/
}
The problem i have is with this line:
$cleanedParams = $security->clean($params->fromRoute());
$params->fromRoute()
works most of the times but sometimes when i call my application from 3rd party api i get this error.
Controllers must implement Zend\Mvc\InjectApplicationEventInterface to use this plugin
I don't see anything that is different in calls. I just need a clarification what this error represents so if i need to change the design of application, i should do so.
It is just a guess, but I think that the params controller plugin which you are using via service manager has no controller assigned in some cases, thats why you should only use it inside a controller. You should be able to debug it at https://github.com/zendframework/zendmvc/blob/master/src/Controller/Plugin/Params.php#L110.
Maybe this happens if you access an invalid route or something like this.
Futhermore in my opinion you should get the params from the current route match instead of the params controller plugin, like
$event->getRouteMatch()->getParams();
Indeed you should check wheter a routematch exists before using it.