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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突