Good day,
I am lost on how to implement what I want in ZF2 since I'm new to it. I have a site that uses ZF2 and ZfcTwig module and I want my TWIG template to have a dynamic navigation based on the settings placed in my mysite.global.php. I want it to read the contents of my config array and create the main navigation using the contents and this will be implemented to all pages of my site.
I already created my layout.twig:
<div class="nav-collapse collapse">
<ul class="nav">
{% for item in navigation %}
<li {% if item.active %} class="active" {% endif %}>
<a href="{{ url(item.route) }}">
{{ translate(item.name) }}
</a>
</li>
{% endfor %}
</ul>
</div><!--/.nav-collapse -->
and my mysite.global.php
return array(
'mysite' => array(
'navigation' => array(
'home' => array(
'name' => 'Home',
'route' => 'home',
),
'profile' => array(
'name' => 'Profile',
'route' => 'myroute',
'active' => true
),
)
)
);
This is where I'm lost. How can I transfer my settings to my layout.twig so that every page that uses the layout will use the same items? I tried this code and placed it to my Module.php in the onBootstrap() method:
//get my settings and place it to $navigation then
$view = $e->getApplication('application')->getMvcEvent()->getViewModel();
$view->navigation = $navigation;
but it doesn't work. {{ navigation }} still contains an empty array. What am I doing wrong?
The solution which I used:
in module.config.php
'service_manager' => array(
'factories' => array(
'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
'Navigation' => 'Zend\Navigation\Service\DefaultNavigationFactory',
),
),
.....
'navigation' => array('default' =>array(
array('label' => 'Home', 'route' => 'home', 'pages' => array(
array('label' => 'Profile', 'route' => 'myroute'),
),
),
),
),
And then in layout.twig
{{ navigation('navigation').menu() | raw }}
and
{{ navigation('navigation').breadcrumbs().render('navigation') | raw }}
And that's all, it should work and by the way, it uses Navigation helper