MVC路由和url视图助手

时间:2013-02-26 01:36:24

标签: zend-framework2

我对MVC路由有疑问。

我在module.config.php中设置了我的路线,如下所示:

'router' => array(
'routes' => array(
    'album' => array(
        'type'    => 'segment',
        'options' => array(
            'route'    => '/album[/page/:page]',
            'constraints' => array(
                'page'   => '[0-9]+',
            ),
            'defaults' => array(
                'controller' => 'Trade\Controller\Album',                                       
                'action'     => 'index',
                'page'       => 1,
            ),
        ),
        'may_terminate' => true,
        'child_routes' => array(
            'default' => array(
                'type'    => 'segment',
                'options' => array(
                    'route'    => '[/:action][/:id]',
                    'constraints' => array(
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'id'     => '[0-9]+',
                    ),
                ),
            ),
        ),
    ),

拥有以下路线:

/album
/album/page/2
/album/edit/3
/album/add

在视图脚本中,我使用:

<a href="<?php echo $this->url('album/default', array('action'=>'edit', 'id' => $album->id));?>">Edit</a>

导致

/album/page/1/edit/2

这不是我想要的网址。我希望网址看起来像     / album / edit / 3

由于我没有在url参数数组中提供页码,因此我没想到会获取默认页面。

我确信有一种更智能的方法可以设置所需的路由,并且非常感谢任何指针。

彼得

1 个答案:

答案 0 :(得分:0)

module.config.php 中更改路线,如下所示

'router' => array(
    'routes' => array(
        'album' => array(
            'type'    => 'Literal',
            'options' => array(
                'route'    => '/album',
                'defaults' => array(
                    'controller' => 'Trade\Controller\Album',
                    'action' => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                'add' => array(
                    'type' => 'Literal',
                    'options' => array(
                        'route' => '/add',
                        'defaults' => array(
                            'action' => 'addalbum',
                        ),
                    ),
                ),
                'edit' => array(
                    'type' => 'Segment',
                    'options' => array(
                        'route' => '/edit[/:id]',
                        'constraints' => array(
                            'id' => '[0-9]+',
                        ),
                        'defaults' => array(
                            'action' => 'editalbum',
                        ),
                    ),
                ),
                'page' => array(
                    'type' => 'Segment',
                    'options' => array(
                        'route' => '/page[/:id]',
                        'constraints' => array(
                            'id' => '[0-9]+',
                        ),
                        'defaults' => array(
                            'action' => 'pagealbum',
                        ),
                    ),
                ),
            ),
        ),
    ),
),

现在将视图脚本中的链接更改为

<a href="<?php echo $this->url('album/edit', array('id'=>$album->id)); ?>">Edit</a>

您的路线现在可以。