我正在尝试从 ZF3 中的容器实现我的 zend 导航。我已经通过这个快速入门教程成功地创建了导航,直接在config/autoload/global.php
orconfig/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
是什么,所以我假设是我$navigation
的Module.php。问题是,因为是一个数组,它会抛出错误。问题是:
- 我究竟做错了什么?
- 这个
$view
教程是从哪里来的? - 我应该从我的Module.php传递什么来让它工作?
提前致谢!