I'm trying to implement my zend navigation from a container in ZF3. I have successfully created navigation with this quick start tutorial introducing navigation directly in config/autoload/global.php
or config/module.config.php
files:
https://docs.zendframework.com/zend-navigation/quick-start/
But now I need to make it work these with the helpers to allow navigation modifications from the controller, using the "Navigation setup used in examples" section:
https://docs.zendframework.com/zend-navigation/helpers/intro/
This is my 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()
);
},
],
];
}
}
And this is my 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([
]);
}
}
But then I get the following error:
Fatal error: Call to a member function plugin() on array in /var/www/html/zf3/module/Application/src/Controller/IndexController.php on line 50
In the tutorial they use the following statement:
// Store the container in the proxy helper:
$view->plugin('navigation')->setContainer($container);
// ...or simply:
$view->navigation($container);
But I don“t know what this $view
is, so I assume is my $navigation
from my Module.php. The problem is that, because is an array, it throws the error. The questions are:
- What am I doing wrong?
- Where this
$view
of the tutorial come from? - What I should pass from my Module.php to get it work?
Thanks in advance!