[Dancer-users] Multiple "optional" components in URLs

Ovid curtis_ovid_poe at yahoo.com
Thu Apr 19 12:51:41 CEST 2012


Hi all,

I had this in my Dancer code:

  get '/instance/?:id?' => sub {
      my $id = param('id');
      ...
  };

That let's me do this:

  http://localhost:5000/instance      # get a list of instances
  http://localhost:5000/instance/     # get a list of instances

  http://localhost:5000/instance/213  # get instance 213


However, after I typoed a URL, I discovered that it would also match:

  http://localhost:5000/instances     # get instance 's'
 

This is because of the generated regex:

  /^\/instance\/?([^\/]+)?$/

I've fixed this by switching to regex paths, but I would argue for the following to be the regex generated from my example above:

  /^\/instance(?:\/([^\/]+))?$/


In other words, if you have multiple "optional" components in a path, they are all required up to, but excluding, the last optional component found in the URL that is accessed. You could then safely visit /instance, /instance/, and /instance/42.

This would allow the easy construction of things like this:

  /member/?:member_id?/?:tag_id?

Allowing you to access:

  /member
  /member/
  /member/3
  /member/3/
  /member/3/7

This would make it easy to write the sort of URLs that I often write.

Speculation: at that point, if we have an optional component, we could *assume* that the preceding / must be optional (?), leading to a simple URL:

  /member/:member_id?/:tag_id?

Once something optional is found, nothing after it may be mandatory (otherwise, if you had optional items collapsing out of the URL, it would be a semantic mess that's impossible to sort through).

Thoughts?

Cheers,
Ovid
--
Live and work overseas - http://www.overseas-exile.com/
Buy the book           - http://www.oreilly.com/catalog/perlhks/
Tech blog              - http://blogs.perl.org/users/ovid/
Twitter                - http://twitter.com/OvidPerl/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.backup-manager.org/pipermail/dancer-users/attachments/20120419/99c7643c/attachment.htm>


More information about the Dancer-users mailing list