简体   繁体   English

ZF2转换器到控制器

[英]ZF2 translator into controller

I am trying to use the translator inside my application as showen below in my example but I am stuck with the error. 我试图在我的应用程序中使用转换程序,如下面的示例所示,但是我一直卡在错误中。

module.config.php module.config.php

'controllers' => array(
        'factories' => array(
            'Application\Controller\AjaxController' => function($sm) {
               $translator = $sm->getServiceLocator()->get('translator');
               $controller = new Application\Controller\AjaxController($translator);
         }
  ),

AjaxController.php AjaxController.php

use Zend\Mvc\Controller\AbstractActionController;
use Zend\I18n\Translator\Translator;
use Zend\View\Model\JsonModel;
use Zend\Session\Container;
use Zend\Debug\Debug;

class AjaxController extends AbstractActionController
{
    protected $translator;

    public function __construct(Translator $translator)
    {
        $this->translator = $translator;
    }

Error ouput 错误输出

Catchable fatal error: Argument 1 passed to Application\Controller\AjaxController::__construct() 
must be an instance of Zend\I18n\Translator\Translator, instance of 
Zend\Mvc\I18n\Translator given, called in 
*****/module/Application/config/module.config.php on line 515 and defined in 
*****/module/Application/src/Application/Controller/AjaxController.php on line 26

From the error message it seems that you are using the wrong translator. 从错误消息看来,您使用的翻译器错误。 Try to change your use clause from Zend\\I18n\\Translator\\Translator to Zend\\Mvc\\I18n\\Translator . 尝试将您的use子句从Zend\\I18n\\Translator\\Translator更改为Zend\\Mvc\\I18n\\Translator

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM