2015-10-14 31 views
1

我的cli控制器給翻譯者帶來了錯誤。ZF2 setTranslator上的致命控制檯錯誤

Fatal error: Call to undefined method Zend\Mvc\Router\Console\SimpleRouteStack::setTranslator() in .. 

哪裏出了問題

設置路由變換器

public function onPreRoute($e) 
{ 
    $application = $e->getTarget(); 
    $serviceManager = $application->getServiceManager(); 
    $serviceManager->get('router')->setTranslator($serviceManager->get('translator')); 
} 

通過這個混帳問題跟蹤我注意到有一個問題,因爲它是不兼容的功能。我的問題是,我該如何防止cli模塊嘗試設置翻譯器,因爲它現在引發了一個致命錯誤。

Git的網址:https://github.com/doctrine/DoctrineORMModule/issues/333

感謝

+0

'回聲\ Zend的\調試\調試::轉儲(get_class_method(0函數$ serviceManager-> GET ('router')),null,false);'顯示沒有setTranslator方法。 – Stanimir

回答

1

我有這個問題的一次,因爲CLI申請的路徑是不一樣的對象作爲標準的請求對象(顯然)

所以Zend\Http\Request不等於Zend\Console\Request

它不是相同的模塊,再加上它不是事件相同的結構,事件如果它們實現相同的接口,有些方法不存在。

Zend\Http\Request extends AbstractMessage(這個擴展消息) Zend\Console\Request extends Message直接。

你的問題是一個很好的例子,我們也可以談談 $request->getUri()這樣也行不通。

爲了防止這種情況,我有一個解決方案,但它不像我想要的那麼優雅,但它的工作原理。如果有人有更優雅的解決方案,歡迎您分享。

因此,解決辦法是:

if (php_sapi_name() !== 'cli' && $request->getServer('HTTP_X_FORWARDED_FOR', false)) { 

// Your code that not compliant with console routes 
} 

您也可以使用此conditionnal聲明:

if (!($request instanceof \Zend\Console\Request) and !$request->isXmlHttpRequest()) { 
// Your code that not compliant with console routes or ajax calls 
} 

爲了找到這個解決方案,我用了Zend框架應用程序的啓發。 In their index.php他們這樣做:

if (php_sapi_name() === 'cli-server') { 
    $path = realpath(__DIR__ . parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)); 
    if (__FILE__ !== $path && is_file($path)) { 
     return false; 
    } 
    unset($path); 
} 

我剛纔看到他們做,因爲我的它第一次下載了一些變化;)