dongtan7639
2013-01-30 07:59
浏览 30
已采纳

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

Here is my dump for route upon which i want to route my app.

[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
                    )

But when i try this

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

i get following error.

Route with name "Album\index" not found

 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',
                ),
            ),
        ),
    ),
),

EDIT: As advised i made following changes

  '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(
                        ),
                    ),
                ),
            ),
        ),
    ),
),

but now i cant seem to pass index action. I mean the only action of album module visible to me is index and none else can some 1 correct/suggest what must be done?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douxian1895 2013-01-30 09:30
    已采纳

    As i guessed, a route Album\index does not exist. This is your configuration:

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

    What you're looking for is this:

    // 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);
    

    The Syntax you used is for child_routes. See this example

    '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'
                            )
                        )
                    )
                )
            )
        )
    )
    

    And the associated routing:

    //AlbumController::indexAction()
    $this->url('album');
    
    //AlbumController::listAction()
    $this->url('album/list');
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题