php - 导航面包屑未在 zend Framework 2 中呈现

标签 php zend-framework2

我是 zend framwork 的新手,我根据此链接 ( http://zf2.readthedocs.org/en/latest/tutorials/tutorial.navigation.html ) 进行操作,但面包屑没有显示在 View 或布局中。 下面是我已经实现的代码。指导我我做错了什么?

在 module.config.php 中,

return array(
     ........
     'router' => array(
        'routes' => array(
            'admin' => array(
                'type'    => 'segment',
                'options' => array(
                    'route'    => '/admin[/][:action][/:id]',
                    'constraints' => array(
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'id'     => '[0-9]+',
                    ),
                    'defaults' => array(
                        'controller' => 'Admin\Controller\Admin',
                        'action'     => 'index',
                    ),
                ),
                'may_terminate' => true,
                 'child_routes' => array(
                    'settings' => array(
                        'type'    => 'Segment',
                        'may_terminate' => true,
                        'options' => array(
                            'route'    => '/general[/][:action][/][:id]',
                            'constraints' => array(
                                'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                'id'     => '[0-9]+',
                            ),
                            'defaults' => array(
                                'controller' => 'Admin\Controller\Settings\General',
                                'action'     => 'index',
                            ),
                        ),
                    ),
                ),
            ),
            'dashboard' => array(
                'type'    => 'segment',
                'options' => array(
                    'route'    => '/admin/dashboard[/][:action][/:id]',
                    'constraints' => array(
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'id'     => '[0-9]+',
                    ),
                    'defaults' => array(
                        'controller' => 'Admin\Controller\Dashboard',
                        'action'     => 'index',
                    ),
                ),
            ), 
        ),
    ),
    'navigation' => array(
        'default' => array(
            array(
                'label' => 'Home',
                'route' => 'admin/dashboard',
            ),
            array(
                'label' => 'Admin',
                'route' => 'admin/settings/general/',
                'pages' => array(
                    array(
                        'label' => 'General',
                        'route' => 'admin/settings/general/',
                        'action' => 'index',
                    ),
                    array(
                        'label' => 'Web',
                        'route' => 'admin/settings/general/',
                        'action' => 'web',
                    ),
                ),
            ),
        ),
    ),
'view_manager' => array(
        'template_map' => array(
            'layout/sidemenu'           => __DIR__ . '/../view/admin/common/sidemenu.phtml',  
            'layout/breadcrumbs'           => __DIR__ . '/../view/admin/common/breadcrumbs.phtml', 
            'error/404'               => __DIR__ . '/../view/error/404.phtml',
            'error/index'             => __DIR__ . '/../view/error/index.phtml',
        ),
        'template_path_stack' => array(
            'admin' => __DIR__ . '/../view',
        ),
        'display_not_found_reason' => true, 
        'display_exceptions'       => true,
        'not_found_template'       => 'error/404', 
        'doctype'                  => 'HTML5',
        'exception_template'       => 'error/index',
    ), 
    'service_manager' => array(
        'factories' => array( 
            'navigation' => 'Zend\Navigation\Service\DefaultNavigationFactory',  
        ),
    ),
    ......
);

在layout.phtml中

$this->navigation('navigation')
               ->breadcrumbs()
               ->setMinDepth(0)
               ->setPartial('layout/breadcrumbs');

最佳答案

请将路由条件“admin/dashboard”更改为“admin/default”。如果它仍然不起作用,那么您可以通过在 module.php 中实现以下代码来捕获matchedRoute:

/* put these lines on top after namespace */

use Zend\Mvc\ModuleRouteListener;

use Zend\Mvc\MvcEvent;

/* put these functions in class */

public function onBootstrap(MvcEvent $e)
{
    $eventManager = $e->getApplication()->getEventManager();
    $eventManager->attach('route', array($this, 'onRouteFinish'), -100);
}

public function onRouteFinish($e)
{
     $matches    = $e->getRouteMatch();
     $controller = $matches->getParam('controller');
     var_dump($matches);die();
}

结果,请检查 ["matchedRouteName":protected] 的值并相应地更改 module.config.php 中的路由条件。祝你好运..!!

关于php - 导航面包屑未在 zend Framework 2 中呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18977389/

相关文章:

php - 如何使用 Zend 服务亚马逊?

php - 使用php获取货币符号

php - 用PHP远程查询WMI

php - ZEND - 如何让它在共享主机上运行?

php - 如何修复 Class 'Album\Controller\AlbumController' not found 错误?

php - 如何在不使用 ZF2.. 中的任何 Controller 对象的情况下访问 module.php 中的 Controller 插件?

php - Laravel 5 如何全局设置 Cache-Control HTTP header ?

php - 很难查询数据库

php - Laravel Forbidden 您无权访问此服务器上的/storage/

zend-framework2 - 如何在ZF2的tableGateway中使用has()