问题
I'm struggling to connect the dots between the ZF1 way of initializing things in a bootstrap, and the ZF2 way of injecting things from a config file (seemingly).
To wit, in ZF1, I had something like this in my boostrap:
protected function _initNavigation()
{
$this->bootstrap('layout');
$this->bootstrap('view');
$navigation = new Zend_Navigation();
// ...code to add pages...
$layout = $this->getResource('layout');
$view = $layout->getView();
$view->navigation($navigation);
}
In ZF2, I'm not even sure what to start looking for, to accomplish something similar.
I've read the posts that refer to:
public function onBootstrap (Event $e)
{
}
and the way that you can do things like:
$application = $e->getApplication();
$services = $application->getServiceManager();
But, what's the equivalent of:
$layout = $this->getResource('layout');
$view = $layout->getView();
$view->navigation($navigation);
Would I do this in Module, or is it better done in the config file and injected? If injected, how?
I've read Rob Allen's tutorial, and have been searching the net for examples of things that go beyond tutorial-level code. The things that I've found (like other ZF2 modules) have been more oriented toward being working modules (understandably), than being examples through to convey the nuances to others... Since I can't find much on this topic, I'm assuming that there's some small, fundamental thing I'm missing that -- when I see it -- will have it all make sense.
回答1:
'service_manager' => array(
'factories' => array(
'Navigation' => 'Zend\Navigation\Service\DefaultNavigationFactory',
),
),
Add up this line in your module config and it will work.
回答2:
Simple example howto make a navigation
file path module/Application/config/module.config.php
<?php
return array(
'router' => array(
'routes' => array(
'home' => array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'Application\Controller\Index',
'action' => 'index',
),
),
),
'default' => array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '/[:namespace[/:controller[/:action]]]',
'constraints' => array(
'namespace' => '[a-zA-Z][a-zA-Z0-9_-]*',
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
//'locale' => 'da_DK',
'namespace' => 'Application',
'controller' => 'index',
'action' => 'index',
),
),
),
),
),
'controllers' => array(
'invokables' => array(
'index' => 'Application\Controller\IndexController',
'Application\Controller\Index' => 'Application\Controller\IndexController'
),
),
'service_manager' => array(
'factories' => array(
'Navigation' => 'Zend\Navigation\Service\DefaultNavigationFactory',
),
),
'view_manager' => array(
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_map' => array(
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
),
'template_path_stack' => array(
__DIR__ . '/../view',
),
),
);
Next is the navigation configuration
<?php
/*
* This file path config/autoload/application.global.php
*/
return array(
// All navigation-related configuration is collected in the 'navigation' key
'navigation' => array(
// The DefaultNavigationFactory we configured in (1) uses 'default' as the sitemap key
'default' => array(
// And finally, here is where we define our page hierarchy
'home' => array(
'label' => 'Home',
'route' => 'home',
),
'news' => array(
'label' => 'News',
'controller' => 'news',
'action' => 'news',
'route' => 'default',
'pages' => array(
'add' => array(
'label' => 'Add news',
'controller' => 'news', /* or create a seperate route insteed*/
'action' => 'add',
'route' => 'default',
),
),
),
),
),
);
And last echo navtigation layout file or view file
example module/Application/view/layout/layout.phtml
<?php echo $this->navigation('Navigation')->menu(); ?>
来源:https://stackoverflow.com/questions/11551736/zf2-way-to-set-up-a-navigation-bar