Zf2查看具有相同参数的帮助程序URL子路由

时间:2014-09-25 10:52:12

标签: php zend-framework2

当父路线和子路线有共同/相同的参数时,我无法使用URL view-helper创建正确的URL。

我的网址配置:

'contact' => array(
    'type' => 'Segment',
    'options' => array(
        'route' => '/contact[/:contact][/action/:action]',
        'constraints' => array(
            'contact' => '[0-9]+',
            'action'  => '[a-zA-Z_-]+',
        ),
        'defaults' => array(
            'controller' => 'CrmContact',
            'action'     => 'index',
        ),
    ),
    'may_terminate' => true,
    'child_routes' => array(
        'task' => array(
            'type' => 'Segment',
            'options' => array(
                'route' => '/task[/:task][/action/:action]',
                'constraints' => array(
                    'task' => '[0-9]+',
                    'action'  => '[a-zA-Z_-]+',
                ),
                'defaults' => array(
                    'controller' => 'CrmTask',
                    'action'     => 'index',
                ),
            ),
        ),
    )
)

很明显,父路由和子路由都包含参数“action”,当直接调用时,此路由完全正常,这意味着如果您使用'/ contact / 1 / task / 1 / action / edit'访问浏览器是上帝。尝试使用viewhelper Url构建此URL时出现问题

$this->url('contact/task', array('contact' => $contact->id,'task' => $task->id, 'action' => 'edit'))

这会产生错误的网址,即

/contact/1/action/edit/task/1    instead of 
/contact/1/task/1/action/edit
帮助程序基本上从子路由劫持动作参数并将其用于父路由...我不能停止使用参数'动作',因为它是跨越控制器方法的路由请求的框架方式的一部分。 ..

2 个答案:

答案 0 :(得分:1)

您的路线联系和您的子路线任务与不同的控制器相关联。所以你可以这样分开它们。

 'contact' => array(
    'type' => 'Segment',
    'options' => array(
        'route' => '/contact[/:contact][/action/:action]',
        'constraints' => array(
            'contact' => '[0-9]+',
            'action' => '[a-zA-Z_-]+',
        ),
        'defaults' => array(
            'controller' => 'crm-contact',
            'action' => 'index',
        ),
    ),
),
'task' => array(
    'type' => 'Segment',
    'options' => array(
        'route' => '/task[/:task][/action/:action]',
        'constraints' => array(
            'task' => '[0-9]+',
            'action' => '[a-zA-Z_-]+',
        ),
        'defaults' => array(
            'controller' => 'crm-task',
            'action' => 'index',
        ),
    ),
),

默认路由也是控制器/操作。所以单独的控制器通常在不同的路径中。 如果您需要在任务路线中显示联系人ID,则可以将路线更改为
'route' => 'contact/:contact/task[/:task][/action/:action]', 您也可以将任务和联系人约束重命名为task_id和contact_id,以便清楚。

答案 1 :(得分:1)

我想保留的原因"任务"作为孩子的路线"联系"因为任务只有在接触时才有意义,它基本上是多对一的关系。我通过在更多子路线中打破它来解决它:

'contact' => array(
    'type' => 'Segment',
    'options' => array(
        'route' => '/contact[/:contact]',
        'constraints' => array(
            'contact' => '[0-9]+',
        ),
        'defaults' => array(
            'controller' => 'CrmContact',
            'action'     => 'index',
        ),
    ),
    'may_terminate' => true,
    'child_routes' => array(
        'contact_action' => array(
            'type' => 'Segment',
            'options' => array(
                'route' => '/action/:action',
                'constraints' => array(
                    'contact' => '[0-9]+',
                    'action'  => '[a-zA-Z_-]+',
                ),
                'defaults' => array(
                    'controller' => 'CrmContact',
                    'action'     => 'index',
                ),
            ),
        ),
        'task' => array(
            'type' => 'Segment',
            'options' => array(
                'route' => '/task[/:task]/action/:action',
                'constraints' => array(
                    'task' => '[0-9]+',
                    'action'  => '[a-zA-Z_-]+',
                ),
                'defaults' => array(
                    'controller' => 'CrmTask',
                    'action'     => 'index',
                ),
            ),
        ),
    )
),