Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ZF2 | Routing post data using Zend\Mvc\Router\Http\Method

Routing post data in ZF2

I have tried to set up a routing in zf2 where all post data of the route /connection/add is routing to a separate method using this yaml configurations:

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

Everything in the above example works just fine, except the deepest child post that is using the type of Zend\Mvc\Router\Http\Method

Expected output:

When one is submitting post data to the rout /connection/add, that person will be routed to the test action.

Actual output:

The last child in the above routing is ignored and the add action is still invoked upon dispatching post data sent from a form.

Question:

  • What am I missing?
  • Is there a way to have this kind of routing in my application?
  • If so, how could the configuration look?
like image 921
superhero Avatar asked Dec 11 '25 14:12

superhero


1 Answers

It actually is possible, it just requires a little more explicit configuration.

The reason your example wasn't working is because the router matched your 'add' route successfully and simply returned there without looking ahead. You have to tell it that it can't terminate there by setting 'may_terminate' to false and explicitly defining all methods you want to deal with in the 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

Remember, the key is to set 'may_terminate' to false so the router doesn't return a match too early.

like image 60
Matt Pinkston Avatar answered Dec 16 '25 09:12

Matt Pinkston