我通过将以下代码添加到您的 module.сconfig.php 解决了这个问题。
return array(
'view_helpers' => array(
'factories' => array(
'langWidget' => function($sm){
return new \Application\View\Helper\LanguagesWidget($sm);
}
),
),
);
你可以在你的模板中使用它
<div>{{ langWidget() }}</div>
这是我的视图助手的例子
namespace Application\View\Helper;
use Zend\View\Helper\AbstractHelper;
use Zend\Di\ServiceLocatorInterface;
use Application\Util\Util;
use Zend\View\Model\ViewModel;
/**
* Widget with lang change elements
*
*
*/
class LanguagesWidget extends AbstractHelper
{
protected
/** @var \Zend\ServiceManager\ServiceManager */
$serviceLocator;
public function __construct($sm)
{
$this->serviceLocator = $sm->getServiceLocator();
$this->viewManager = $this->serviceLocator->get('ViewManager');
}
public function __invoke()
{
$config = $this->serviceLocator->get('Config');
$appLangs = Util::getSafeArrayItemValue($config, 'languages', array());
$widget = new ViewModel(array(
'langs' => $appLangs
)
);
$widget->setTemplate('widget/langs');
return $this->viewManager->getRenderer()->render($widget);
}
}