面包屑。 ZF2

时间:2015-07-28 12:07:46

标签: php zend-framework2 breadcrumbs

我阅读了官方文档,但我并不了解如何创建面包屑。这就是我在module.config中的内容:

'gallery' => array(
            'type'    => 'Segment',
            'options' => array(
                'route'    => '/gallery[/:id]',
                'constraints' => array(
                    'id' => '[0-9]+',
                ),
                'defaults' => array(
                    '__NAMESPACE__' => 'Home\Controller',
                    'controller' => 'Gallery',
                    'action' => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                'item' => array(
                    'type'    => 'Segment',
                    'options' => array(
                        'route'    => '/item[/:id]',
                        'constraints' => array(
                            'id' => '[0-9]+',
                        ),
                        'defaults' => array(
                            '__NAMESPACE__' => 'Home\Controller',
                            'controller' => 'Gallery',
                            'action' => 'item',
                        ),
                    ),
                ),
            ),
        ),
'navigation' => array(
    'default' => array(
        array(
            'label' => 'Gallery',
            'route'   =>  'gallery',
            'pages' => array(
                array(
                    'label' => 'Gallery1',
                    'route' => 'gallery/1/item/',
                    'action' => 'index',
                ),
                array(
                    'label' => 'Web',
                    'route' => 'gallery/2/item/',
                    'action' => 'index',
                ),
            ),
        ),
    ),
),

这在item.php中:

$this->navigation('navigation')
            ->breadcrumbs()
            ->setMinDepth(0)
            ->setPartial('partial/breadcrumb.phtml');

这是在breadcrumb.phtml:

<ul class="breadcrumb">
<?php

foreach ($this->pages as $key => $page):
    ?>
    <li>
        <?php
        if ($key < count($this->pages) - 1):
            ?>
            <a href="<?php echo $page->getHref(); ?>"><?php echo $page->getLabel(); ?></a>
            <?php

        else:
            ?>
            <?php echo $page->getLabel(); ?>
        <?php endif; ?>
    </li>
<?php endforeach; ?>

那不行。有人知道如何解决它吗?

1 个答案:

答案 0 :(得分:0)

您的配置无效。其他没问题。

'router' => array(
    'routes' => array(
        'gallery' => array(
            'type'    => 'Segment',
                'options' => array(
                    'route'    => '/gallery[/:gallery_id][/]', /* Gallery id */
                    'constraints' => array(
                        'gallery_id' => '[0-9]+',
                    ),
                    'defaults' => array(
                        '__NAMESPACE__' => 'Home\Controller',
                        'controller' => 'Gallery',
                        'action' => 'index',
                    ),
                ),
                'may_terminate' => true,
                'child_routes' => array(
                    'item' => array(
                        'type'    => 'Segment',
                        'options' => array(
                            'route'    => 'item[/:item_id][/]', /* Item id */
                            'constraints' => array(
                                'item_id' => '[0-9]+',
                            ),
                            'defaults' => array(
                                '__NAMESPACE__' => 'Home\Controller',
                                'controller' => 'Gallery',
                                'action' => 'item',
                            )
                        )
                    )
                )
            )
        )
    )
),
'navigation' => array(
    'default' => array(
        array(
            'label' => 'Gallery',
            'route'   =>  'gallery',
            'pages' => array(
                array(
                    'label' => 'Gallery1',
                    'route' => 'gallery/item', /* Route name */
                    'action' => 'index',
                    'params' => array( /* Route parameters */
                        'gallery_id' => '1', 
                    ),
                ),
                array(
                    'label' => 'Web',
                    'route' => 'gallery/item',
                    'action' => 'index',
                    'params' => array(
                        'gallery_id' => '2',
                    )
                )
            )
        )
    )
)