0
votes

I am Migrating from ZF2 to ZF3 , installed PHP7.0 and ZF3 Sucees fully. Placed the ZF2 code base in to ZF3 framework and installed all dependencies through json. Now i am Facing Issue related to Databse Translations. The error i facing was

Could any one help me out

Fatal error: Uncaught Zend\ServiceManager\Exception\ServiceNotFoundException: A plugin by the name "database" was not found in the plugin manager Zend\I18n\Translator\LoaderPluginManager in /var/www/html/skeleton-application/vendor/zendframework/zend-servicemanager/src/AbstractPluginManager.php:131 Stack trace:

In ZF2 i implemented DB Translations.

1)Application/module.config.php as

'translator' => array(
    'loaderpluginmanager' => array(
        'factories' => array(
            'database' => '\Charan\I18n\Translator\Loader\DatabaseTranslatorFactory',
        ),
    ),
    'locale' => 'en_US',
    'remote_translation' => array(
        array(
            'type' => 'database',
        ),
    ),
    'cache' => array(
        'adapter' => array(
            'name' => 'Filesystem',
            'options' => array(
                'cache_dir' => __DIR__.'/../../../../../data/cache',
                'ttl' => '3600',
                'namespace' => \Charan::config()->site->slug
            ),
        ),
        'plugins' => array(
            array(
                'name' => 'serializer',
                'options' => array(),
            ),
            'exception_handler' => array(
                'throw_exceptions' => true,
            ),
        ),
    ),
),
  1. Charan\library\core\I18n\Translator\Loader\DatabaseTranslatorFactory.php as

    namespace Charan\I18n\Translator\Loader;

    use Zend\ServiceManager\FactoryInterface;
    use Zend\ServiceManager\ServiceLocatorInterface;
    
    class DatabaseTranslatorFactory implements FactoryInterface
    {
        public function createService(ServiceLocatorInterface $serviceLocator)
        {
            return new DatabaseTranslator();
        }
    }
    
  2. Application\Module.php as below

    public function onBootstrap(MvcEvent $e){
    
    // Set the application variable
    $app = $e->getApplication();
    
    // Set the event and service manager variables
    $eventManager = $app->getEventManager();
    $serviceManager = $app->getServiceManager();
    $adapter = $serviceManager->get('Zend\Db\Adapter\Adapter');
    \Zend\Db\TableGateway\Feature\GlobalAdapterFeature::setStaticAdapter($adapter);
    $moduleRouteListener = new ModuleRouteListener();
    $moduleRouteListener->attach($eventManager);
    
    // Add the TemplateInjector class to use custom layouts
    $eventManager->getSharedManager()
        ->attach(
            'Zend\Stdlib\DispatchableInterface',
            MvcEvent::EVENT_DISPATCH,
            new TemplateInjector(),
            -91
        );
    
    $this->initAcl($e);
    $e->getApplication()
        ->getEventManager()
        ->attach('route', array($this, 'checkAcl'));
    
    $e->getApplication()
        ->getEventManager()
        ->getSharedManager()
        ->attach('*', 'route', array($this, 'validateUrl'));
    
    $eventManager->attach('finish', array($this, 'compressOutput'), 100);
    
    // Set static adapter for all modules
    $dbAdapter = $serviceManager->get('Zend\Db\Adapter\Adapter');
    GlobalAdapterFeature::setStaticAdapter($dbAdapter);
    

    }

    public function getServiceConfig()
        {
    
           return array(
                'factories' => array(
                    'MvcTranslator' => 'Charan\Mvc\Service\TranslatorServiceFactory',
                    'StringSanitizer' => function ($sm) {
                        $helper = new Helper();
    
                        return $helper->getStringSanitizer();
                    },
                    'NumericStringSanitizer' => function ($sm) {
                        $helper = new Helper();
    
                        return $helper->getNumericStringSanitizer();
                    },
                    'EmailSanitizer' => function ($sm) {
                        $helper = new Helper();
    
                        return $helper->getEmailSanitizer();
                    },
                    'Zend\Db\Adapter\Adapter' => function($serviceManager) {
                        if (\Charan::isDevelopment()) {
    
                            $adapter = new ProfilingAdapter(array(
                                'driver'    => 'Pdo',
                                'dsn'       => 'mysql:dbname=' . \Charan::config()->database->name . ';host=' . \Charan::config()->database->host,
                                'database'  => \Charan::config()->database->name,
                                'username'  => \Charan::config()->database->username,
                                'password'  => \Charan::config()->database->password,
                                'hostname'  => \Charan::config()->database->host
                            ));
    
                            if (Cli::isCli()) {
                                $logger = new Logger();
                                $writer = new Stream('php://output');
                                $logger->addWriter($writer, Logger::DEBUG);
                                $adapter->setProfiler(new LoggingProfiler($logger));
                            } else {
                                $adapter->setProfiler(new Profiler());
                            }
    
                            if (isset($databaseParameters['options']) && is_array($databaseParameters['options'])) {
                                $options = $databaseParameters['options'];
                            } else {
                                $options = array();
                            }
    
                            $adapter->injectProfilingStatementPrototype($options);
    
                        } else {
                            $adapterFactory = new AdapterServiceFactory();
                            $adapter = $adapterFactory->createService($serviceManager);
                        }
    
                        GlobalAdapterFeature::setStaticAdapter($adapter);
                        return $adapter;
                    },
                    'translator' => function (\Zend\ServiceManager\ServiceManager $serviceManager) {
                        $pluginManager = new \Zend\I18n\Translator\LoaderPluginManager();
                        $pluginManager->setServiceLocator($serviceManager);
                        $pluginManager->setFactory('DatabaseTranslatorFactory', function ($serviceManager) {
                            $translator = new \Charan\I18n\Translator\Loader\DatabaseTranslatorFactory();
    
                            return $translator->createService($serviceManager);
                        });
                        $translator = new \Charan\I18n\Translator\Translator(array());
                        $translator->setFallbackLocale('en_GB');
                        $translator->setPluginManager($pluginManager);
                        $translator->addRemoteTranslations('DatabaseTranslatorFactory');
    
                        return $translator;
                    },
                ),
            );
        }
    
1
Hi Charan, did you found a solution ?Abdel

1 Answers

1
votes

A quick guess is that you forgot to migrate your factories from ZF2 to ZF3-compatible, therefore the plugin cannot be created. The signature of FactoryInterface has changed in ZF3, and all factories must be updated in order to work. Check out this link for more info: https://zendframework.github.io/zend-servicemanager/migration/#factoryinterface