【发布时间】:2016-09-08 11:45:13
【问题描述】:
我正在尝试从 ZF3 中的容器实现我的 zend 导航。我已经通过这个直接在config/autoload/global.php 或config/module.config.php 文件中介绍导航的快速入门教程成功地创建了导航:
https://docs.zendframework.com/zend-navigation/quick-start/
但现在我需要使用“示例中使用的导航设置”部分使其与助手一起工作,以允许从控制器修改导航:
https://docs.zendframework.com/zend-navigation/helpers/intro/
这是我的 Module.php
namespace Application;
use Zend\ModuleManager\Feature\ConfigProviderInterface;
use Zend\View\HelperPluginManager;
class Module implements ConfigProviderInterface
{
public function getViewHelperConfig()
{
return [
'factories' => [
// This will overwrite the native navigation helper
'navigation' => function(HelperPluginManager $pm) {
// Get an instance of the proxy helper
$navigation = $pm->get('Zend\View\Helper\Navigation');
// Return the new navigation helper instance
return $navigation;
}
]
];
}
public function getControllerConfig()
{
return [
'factories' => [
$this->getViewHelperConfig()
);
},
],
];
}
}
这是我的 IndexController.php
namespace Application\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Navigation\Navigation;
use Zend\Navigation\Page\AbstractPage;
class IndexController extends AbstractActionController
{
private $navigationHelper;
public function __construct(
$navigationHelper
){
$this->navigationHelper = $navigationHelper;
}
public function indexAction()
{
$container = new Navigation();
$container->addPage(AbstractPage::factory([
'uri' => 'http://www.example.com/',
]));
$this->navigationHelper->plugin('navigation')->setContainer($container);
return new ViewModel([
]);
}
}
然后我收到以下错误:
Fatal error: Call to a member function plugin() on array in /var/www/html/zf3/module/Application/src/Controller/IndexController.php on line 50
在教程中他们使用以下语句:
// Store the container in the proxy helper:
$view->plugin('navigation')->setContainer($container);
// ...or simply:
$view->navigation($container);
但我不知道这个$view 是什么,所以我假设是我的Module.php 中的$navigation。问题是,因为是一个数组,它会抛出错误。问题是:
- 我做错了什么?
- 这个
$view的教程来自哪里? - 我应该从我的 Module.php 传递什么来让它工作?
提前致谢!
【问题讨论】:
标签: php zend-framework zend-navigation zf3