zf2 navigation breadcrumbs paging

2019-08-08 02:16发布

Cannot find the right way to describe sitemap that works with breadcrumbs.

For example i have a navigation described in module.config.php

'navigation' => array(
    'default' => array(
        array(
            'label' => 'Home',
            'route' => 'home'
        ),
        array(
            'label' => 'Articles',
            'route' => 'articles',
            'pages' => array(
                array(
                    'label' => 'Paging',
                    'route' => 'articles',
                    'id' => 'articles',
                    'pages' => array(
                        array(
                            'label' => 'Page 1',
                            'route' => '1'
                        ),
                        array(
                            'label' => 'Page 2',
                            'route' => '2'
                        )
                        ...
                    )
                )
            )
        )
    ),
),

And there i mean it should match patterns like articles/page/1 and articles/page/2 ...

The routes are in autoload/routes.global.php

'articles' => array(
    'type' => 'Zend\Mvc\Router\Http\Literal',
    'options' => array(
        'route'    => '/articles',
        'defaults' => array(
            'controller' => 'Articles\Controller\Index',
            'action'     => 'index',
        ),
    ),
    'may_terminate' => true,
    'child_routes' => array(
        'pagination' => array(
            'type'    => 'Segment',
            'options' => array(
                'route'       => '/page[/:page]',
                'constraints' => array(
                    'page'       => '[0-9A-Za-z]+',
                ),
                'defaults'    => array(
                    'controller' => 'Articles\Controller\Index',
                    'action'     => 'index',
                ),
            ),
        ),
    ),
),

I need to make dynamic breadcrumb paging. So i tried to add example page from controller

$navi = $this->getServicelocator()->get('Navigation');
$page = $navi->findById('articles');
$page->addPage(
    array(
        'label'  => 'Page 10',
        'route' => '10'
    )
);

Breadcrumbs were empty at articles/page/10 ? Is not the right way i am doing this ?

2条回答
做自己的国王
2楼-- · 2019-08-08 02:43

You have to change your "navigation" this way ("pages" array empty) :

'navigation' => array(
'default' => array(
    array(
        'label' => 'Home',
        'route' => 'home'
    ),
    array(
        'label' => 'Articles',
        'route' => 'articles',
        'pages' => array(
            array(
                'label' => 'Paging',
                'route' => 'articles',
                'id' => 'articles',
                'pages' => array()
            )
        )
    )
),

Then, in your controller action you have to add this code :

$navigation = $this->getServiceLocator()->get('Navigation');
$page = $navigation->findBy('id', 'article');
$page->addPage(array(
    'uri' => $this->getRequest()->getRequestUri(),
    'label' => 'Page ' . $pageNumber,
    'active' => true,
));

Like that, all your pages will have their name into the breadcrumb and not a generic term.

查看更多
贼婆χ
3楼-- · 2019-08-08 02:51

Had a Similar Problem, i could resolve it by using the 'useRouteMatch' - flag

'pages' => array(
                        array(
                            'label' => 'Page 1',
                            'route' => '1',
                            'useRouteMatch' => true
                        ),
                        array(
                            'label' => 'Page 2',
                            'route' => '2'
                        )
                        ...
                    )
查看更多
登录 后发表回答