【问题标题】:Zf2 view helper URL child route with same paramsZf2 查看具有相同参数的辅助 URL 子路由
【发布时间】:2014-09-25 10:52:11
【问题描述】:

当父路由和子路由具有共同/相同的参数时,我无法使用 URL 视图助手创建正确的 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' all 访问浏览器是上帝。尝试使用 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

助手基本上从子路由中劫持动作参数并将其用于父路由...我不能停止使用参数“动作”,因为它是跨控制器方法路由请求的框架方式的一部分。 ..

【问题讨论】:

    标签: php zend-framework2


    【解决方案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。

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-10-30
        • 1970-01-01
        • 1970-01-01
        • 2019-10-26
        • 2018-01-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多