Routing: How to create a route with unlimited url segments?

Silverstripe Version: 4.5.1


Hi, I’m really not sure if this kind of problem has already been covered here, as it seems that routing problems presented on this forum vary a lot and are generally quite complex - at least to me :D.

This is what I’m trying to achieve:
A route that recognises a url like this: . The route should always call one single method in a controller class.

I’ve already achieved a solution that recognises a url like this: . But if the url contains more sub segments, I will get a 404 Page not found error.


Name: approutes
  - '#rootroutes'
  - '#coreroutes'
    'go//': MyController


class MyController extends Controller
	private static $allowed_actions = [
		'go' ,
	private static $url_handlers = [
		'$ObjectID!/$OtherOptionalStuff' => 'go',
	public function go()
		// ...

Thank you for your support! :slight_smile: I appreciate it! :slight_smile:

There must be a better way I guess, but can you just do '$ObjectID!/$1/$2/$3/$4/$5/$6/$7/$8' => 'go' for as many params as you may need? $1 - $8 would be optional so that should let you have between 0 and 8 extra segments and still call the go method. That would only work of course if you have a concrete upper limit.

Thanks, I think this is the tradeoff that I will need to make, at least to make it work in the first place. I just need to define parameters up to something like $99 so that the upper limit is high enough. Not clean, but at least will work :). And if someone else comes up with a more elegant solution, I can then switch to that :).

Generally, I would try to get away from this design pattern /for/bar/234/54/234/234234/hello

as it is not very readable and can easily lead to errors.

maybe try:, etc… this means you can have loads of arguments and they are specifically assigned rather than relying on order for meaning.

Of course I do not know your specific situation.

Thank you for your suggestion. I think this would be a good solution in many cases. However, I need to support slashes in this particular case. To explain why, I will need to refine my above example uri in more detail:

I added to the url. So the uri starts with the go part, after which there will be an ID number, and after that there will be a human readable url address to another website, where the controller will redirect the client’s web browser to after handling an action. To be honest, the part and anything after it could be omitted from the url, because the ID number tells the controller where the user should be redirected to. I just want to include the part in the url to give a hint to the user where they will end up if they click a link that has the url

If I would want to, I could convert the url part to one query parameter, but I wouldn’t like it because the slashes of the url would not be so readable anymore:

Just a heads up SS 4.6 will be getting Wildcard URL params which I think is what you’re after… it was only merged in yesterday - NEW: Variadic URL parameter matches for url_handlers (#9438) · silverstripe/silverstripe-framework@1fb574a · GitHub