无法使用Zend Framework 2正确映射路由

时间:2013-01-30 07:59:57

标签: php zend-framework2

这是我想要路由我的应用程序的路径转储。

[album] => Array([route] => Zend\Mvc\Router\Http\Segment Object
                            (
                                [parts:protected] => Array
                                    (
                                        [0] => Array
                                            (
                                                [0] => literal
                                                [1] => /album
                                            )
                                        [1] => Array
                                            (
                                                [0] => optional
                                                [1] => Array
                                                    (
                                                        [0] => Array
                                                            (
                                                                [0] => literal
                                                                [1] => /
                                                            )
                                                        [1] => Array
                                                            (
                                                                [0] => parameter
                                                                [1] => action
                                                                [2] => 
                                                            )
                                                    )
                                            )
                                        [2] => Array
                                            (
                                                [0] => optional
                                                [1] => Array
                                                    (
                                                        [0] => Array
                                                            (
                                                                [0] => literal
                                                                [1] => /
                                                            )
                                                        [1] => Array
                                                            (
                                                                [0] => parameter
                                                                [1] => id
                                                                [2] => 
                                                            )
                                                    )
                                            )
                                    )
                                [regex:protected] => /album(?:/(?P[a-zA-Z][a-zA-Z0-9_-]*))?(?:/(?P[0-9]+))?
                                [paramMap:protected] => Array
                                    (
                                        [param1] => action
                                        [param2] => id
                                    )
                                [defaults:protected] => Array
                                    (
                                        [controller] => Album\Controller\Album
                                        [action] => index
                                    )
                                [assembledParams:protected] => Array
                                    (
                                    )
                            )
                        [priority] => 0
                        [serial] => 3
                    )

但是当我尝试这个时

  $router = $e->getRouter();
  $url = $router->assemble(array(), array('name' => 'Album\index'));

我收到以下错误。

  

未找到名称“相册\索引”的路线

 Edit: here is route settings from module.config

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

编辑:据我所知,我做了以下更改

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

但现在我似乎无法通过索引动作。我的意思是我看到的专辑模块的唯一动作是索引,没有其他任何一个可以纠正/建议必须做什么?

1 个答案:

答案 0 :(得分:1)

正如我猜测的那样,路线Album\index不存在。这是您的配置:

'router' => array(
    'routes' => array(
        'album' => array(  //<-- THIS is the ROUTE_NAME

您正在寻找的是:

// AlbumController::indexAction()
$this->url('album');
$this->url('album', array('action' => 'index');

// AlbumController::listAction()
$this->url('album', array('action' => 'list');

// AlbumController::editAction() with param ID = 3
$this->url('album', array('action' => 'edit', 'id' => 3);

您使用的语法是child_routes。见这个例子

'router'       => array(
    'routes' => array(
        'album'     => array(
            'type'          => 'literal',
            'options'       => array(
                'route'    => '/album',
                'defaults' => array(
                    'controller' => 'album-controller-album',
                    'action'     => 'index'
                )
            ),
            'may_terminate' => true,
            'child_routes'  => array(
                'list'   => array(
                    'type'    => 'literal',
                    'options' => array(
                        'route'    => '/list',
                        'defaults' => array(
                            'action' => 'list'
                        )
                    )
                )
            )
        )
    )
)

以及相关的路由:

//AlbumController::indexAction()
$this->url('album');

//AlbumController::listAction()
$this->url('album/list');