I just upgraded one project to Zend Framework 3 from ZF 1.x version.
In ZF 1.x version, we have implemented a functionality like provided an unique id for a request being made in the application using Zend Registry component, which is not there in ZF 3. I am trying to achieve that functionality using ServiceManager.
So I tried to inject ServiceManager to a Model file by creating a Factory class as below:
My Model Class File code:
<?php
namespace Application\Model\Common;
use Application\Model\Common\Config\BaseConfig;
use Zend\Session\Container;
use \Zend\ServiceManager\ServiceManager;
class CommonUtils extends BaseConfig{
public function __construct(ServiceManager $sm) {
echo '<pre>';
print_r($sm);
exit();
}
}
?>
Factory Class Code:
namespace Application\Model\Common;
use Zend\ServiceManager\Factory\FactoryInterface;
use Interop\Container\ContainerInterface;
class CommonUtilsFactory implements FactoryInterface{
//put your code here
public function __invoke(ContainerInterface $container, $requestedName, mixed $options = null) {
$serviceManager = $container->get('ServiceManager');
return new CommonUtils($serviceManager);
}
}
Updated module.config.php file as below:
/** SERVICE MANAGER CONFIGURATION - STARTS HERE **/
'service_manager' => [
'factories' => [
CommonUtils::class => \Application\Model\Common\CommonUtilsFactory::class
],
// Make an alias so that we can use it where we need
'aliases' => [
],
],
/** SERVICE MANAGER CONFIGURATION - ENDS HERE **/
But it is NOT working and giving below error.
Catchable fatal error: Argument 1 passed to Application\Model\Common\CommonUtils::__construct() must be an instance of Zend\ServiceManager\ServiceManager, none given
Someone please help me here to understand how to access/ inject ServiceManager in Model file in Zend Framework 3.
Thanks in Advance.