在Zend 2中创建导航时,错误URL无效

时间:2015-02-09 11:20:41

标签: zend-framework routes zend-framework2 router

module.config.php

'router' => array(
    'routes' => array(
        'blog' => array(
            'type' => 'Literal',
            'options' => array(
                'route' => '/page',
                'defaults' => array(
                    'controller' => 'Blog\Controller\List',
                    'action' => 'index'
                )
            ),
            'may_terminate' => true,
            'child_routes'  => array(
                'detail' => array(
                    'type' => 'Segment',
                    'options' => array(
                        'route'    => '/:id',
                        'defaults' => array(
                            'action' => 'detail'
                        ),
                        'constraints' => array(
                            'id' => '[0-9]+'
                        )
                    )
                )
            )
        )
    )
),
    ...
        'navigation' => array(
            'default' => array(
                array(
                    'label' => 'Home',
                    'route' => 'home',
                ),
                array(
                    'label'  => 'About Us',
                    'route'  => 'blog',
                    'params' => array('id' => '1'), 
                ),
            ),
        ),
    ...

ListController.php

public function detailAction()
{
    $id = $this->params()->fromRoute('id');

    try {
        $post = $this->postService->findPost($id);
    } catch (\InvalidArgumentException $ex) {
        return $this->redirect()->toRoute('blog');
    }

    return new ViewModel(array(
        'post' => $post
    ));
}

查看:

<?php echo $this->navigation('navigation')->menu()->setUlClass('menu menu-home')->escapeLabels(false); ?>

结果仅显示

<ul class="menu menu-home">
    <li class="active">
        <a href="/">Home</a>
    </li>
    <li>
        <a href="/page">About Us</a>
    </li>
</ul>

如何修复此路线网址,以显示网址http://domain.com/page/1

1 个答案:

答案 0 :(得分:1)

我认为您引用父路由blog而不是子路由blog/detail。因此,即使您指定了一个参数,它也会被忽略,因为父路由不期望/需要一个参数。试试这个:

array(
    'label'  => 'About Us',
    'route'  => 'blog/detail', // instead of 'blog'
    'params' => array('id' => '1'), 
),