Create optionale route parameters

Hi,

how can I define optional route parameters with the Zend Expressive Router. Given is a route like this:

/order/index/index/webspace/770/cpu/4/ram/32/hdd/250

With “webspace”, “cpu” and so on as optional parameters. They can be set but must not. For migrating a ZF1 application to Zend\Expressive the route structure should not be changed.

Any suggestions?

Thanks,

Ralf

Probably needs a custom routing middleware here, and let the existing
router in expressive just match the first bit of the URI.

Thanks,

But how can I let the existing router just match the first bit? Is there a way to stop the matching at a special point? Do you have any example.

To be precise: in the project the Zend Router is used with Zend\Expressive.

Thanks, Ralf

Hey Ralf,

This really depends on the router implementation. I’m looking at
https://github.com/nikic/FastRoute and it should be possible to have a
regex that matches everything after a certain point in the URI.

Marco Pivetta

http://twitter.com/Ocramius

http://ocramius.github.com/

Hi Marco,

but I am afraid Zend\Expressive\Router\ZendRouter does not support regex, does it? Haven’t seen any example in the wilds yet.

I need to check, if the switch to FastRoute is an option.

Thanks, Ralf

I only see a RouteCollector being used:

Marco Pivetta

http://twitter.com/Ocramius

http://ocramius.github.com/

Ok, just noticed that the ZendRouter is using a TreeRouteStack.

I need to look into that a little deeper.

Thanks, Ralf

Have a look at this part:

It creates a segment route type. There is no support yet for regex routes. There is an open issue for regex support or use another router like FastRoute. However, with FastRoute I think an optional parameter can only be followed by other optional parameters in a specific order.

So a regex could match
/order/index/index
/order/index/index/webspace/770
/order/index/index/webspace/770/cpu/4
/order/index/index/webspace/770/cpu/4/ram/32
/order/index/index/webspace/770/cpu/4/ram/32/hdd/250

But you can’t have a regex for:
/order/index/index/webspace/770
/order/index/index/cpu/4/ram/32/hdd/250
/order/index/index/cpu/4/hdd/250

It’s a while since I tried this so I’m not 100% sure.

A post was split to a new topic: Routes are failing with a 404