我很难将ZF1在引导程序中初始化事物的方式和ZF2从配置文件中注入事物的方式(看起来)联系起来。
也就是说,在ZF1中,我的助推器中有这样的东西:
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);
}
在ZF2中,我甚至不知道该开始寻找什么,来完成类似的事情。
我读过的帖子提到:
public function onBootstrap (Event $e)
{
}
以及你可以做的事情的方式,比如:
$application = $e->getApplication();
$services = $application->getServiceManager();
但是,什么等效于:
$layout = $this->getResource('layout');
$view = $layout->getView();
$view->navigation($navigation);
我会在Module中这样做吗,还是在配置文件中更好地这样做并注入?如果注射,如何注射?
我已经阅读了Rob Allen的教程,并且一直在网上搜索超越教程级别代码的例子。我发现的东西(像其他ZF2模块一样)更倾向于成为工作模块(可以理解),而不是通过示例向其他人传达细微差别。。。由于我在这个话题上找不到太多,我假设我错过了一些小的、基本的东西,当我看到它时,一切都会有意义。
'service_manager' => array(
'factories' => array(
'Navigation' => 'ZendNavigationServiceDefaultNavigationFactory',
),
),
将这一行添加到模块配置中,它就可以工作了。
如何进行导航的简单示例
文件路径module/Application/config/module.config.php
<?php
return array(
'router' => array(
'routes' => array(
'home' => array(
'type' => 'ZendMvcRouterHttpSegment',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'ApplicationControllerIndex',
'action' => 'index',
),
),
),
'default' => array(
'type' => 'ZendMvcRouterHttpSegment',
'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' => 'ApplicationControllerIndexController',
'ApplicationControllerIndex' => 'ApplicationControllerIndexController'
),
),
'service_manager' => array(
'factories' => array(
'Navigation' => 'ZendNavigationServiceDefaultNavigationFactory',
),
),
'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',
),
),
);
接下来是导航配置
<?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',
),
),
),
),
),
);
最后一个回波导航布局文件或视图文件
示例模块/Application/view/layout/layout.phtml
<?php echo $this->navigation('Navigation')->menu(); ?>