ZF2 | 使用Zend\Mvc\Router\Http\Method路由发布数据

sup*_*ero 3 url-routing zend-framework2

在ZF2中路由发布数据

我试图在zf2中设置路由,其中​​路由的所有发布数据都/connection/add使用此yaml配置路由到单独的方法:

router:
  routes:
    home:
      type: literal
      options:
        route: '/'
        defaults:
          controller: Admin\Dashboard
          action:     index

    connection:
      type: literal
      options:
        route: '/connection'
        defaults:
          controller: Admin\Connection
          action:     list

      may_terminate: true
      child_routes:
        add:
          type: literal
          options:
            route: '/add'
            defaults:
              action: add

          may_terminate: true
          child_routes:
            post:
              type: method
              options:
                verb: post
                defaults:
                  action: test
Run Code Online (Sandbox Code Playgroud)

上面示例中的所有内容都可以正常工作,除了post使用Zend\Mvc\Router\Http\Method类型的最深的子项

预期产量:

当一个/connection/add人将后期数据提交给溃败时,该人将被路由到该test行动.

实际产量:

忽略上述路由中的最后一个子节点,并add在调度从表单发送的后期数据时仍然调用该操作.

题:

  • 我错过了什么?
  • 有没有办法在我的应用程序中使用这种路由?
  • 如果是这样,配置怎么样?

Mat*_*ton 6

它实际上是可能的,它只需要更明确的配置.

您的示例无法正常工作的原因是路由器成功匹配您的"添加"路由并且只是返回那里而没有向前看.你必须告诉它它不能通过将'may_terminate'设置为false并在child_routes中显式定义你想要处理的所有方法来终止它.

    add:
        type: Literal
        options:
            route: '/add'
            defaults:
                action: add
        may_terminate: false
        child_routes:
            post:
                type: method
                options:
                    verb: post
                    defaults:
                        action: test
            everythingelse:
                type: method
                options:
                    verb: 'get,head,put,delete'
                    defaults:
                        action: add
Run Code Online (Sandbox Code Playgroud)

请记住,关键是将'may_terminate'设置为false,以便路由器不会过早返回匹配.