来自Databse Translations的ZendFramework 3 remote_translation

时间:2018-06-22 13:15:37

标签: php zend-framework zend-framework3

我正在从ZF2迁移到ZF3,完全安装了PHP7.0和ZF3 Sucees。 将ZF2代码库放入ZF3框架,并通过json安装所有依赖项。 现在我正面临与数据库翻译有关的问题。 我面临的错误是

有人可以帮我吗

致命错误:未捕获的Zend \ ServiceManager \ Exception \ ServiceNotFoundException:在/ var / www / html / skeleton-的插件管理器Zend \ I18n \ Translator \ LoaderPluginManager中找不到名为“数据库”的插件应用程序/供应商/zendframework/zend-servicemanager/src/AbstractPluginManager.php:131 堆栈跟踪:

在ZF2中,我实现了DB Translations。

1) Application / module.config.php为

'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

    命名空间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如下

    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 个答案:

答案 0 :(得分:1)

一个快速的猜测是您忘记了将工厂从ZF2迁移到兼容ZF3的方式,因此无法创建该插件。 ZF3中FactoryInterface的签名已更改,必须更新所有工厂才能正常工作。请查看此链接以获取更多信息:https://zendframework.github.io/zend-servicemanager/migration/#factoryinterface