Create optionale route parameters


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


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?



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


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 and it should be possible to have a
regex that matches everything after a certain point in the URI.

Marco Pivetta

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

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

But you can’t have a regex for:

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