ZF2 | Routing mail data using Zend \ Mvc \ Router \ Http \ Method
Routing data in ZF2
I tried to set up routing in zf2 where all post route data is /connection/add
routed to a separate method using these 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 for the deepest child post
which uses the Zend \ Mvc \ Router \ Http \ Method type
Expected Result:
When someone sends postal data to a route /connection/add
, that person will be redirected to action test
.
Actual output:
The last child in the above routing is ignored and the action add
is still called when posting messages sent from the form.
Question:
- What am I missing?
- Is there a way to have routing like this in my application?
- If so, what would the configuration look like?
source to share
This is actually possible, it just requires a little more explicit configuration.
The reason your example doesn't work is because the router successfully completed the add route and just went back there without hesitation. You have to tell him that he cannot terminate this by setting "may_terminate" to false and explicitly defining all the methods you want to deal with in 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 that the key is to set "may_terminate" to false so that the router does not return a match too early.
source to share
Perhaps because you have a child of the add route, perhaps try adding it at the same level and not as a child?
child_routes:
add:
type: literal
options:
route: '/add'
defaults:
action: add
may_terminate: true
post:
type: method
options:
verb: post
defaults:
action: test
may_terminate: true
Make it not a child of the add route, not a brother.
source to share