noodlehaus/dispatch

Can I set optional parameters in route?

Closed this issue · 1 comments

map('/quiz/question/{action}/{?id}', function ($args=null)

I want the id to be an optional parameter in the url, but it isn't working. Also how do I retrieve the queries in the url like site.com/quiz/question/edit?id=123 where I want to retrieve id. I tried params['id'] and args['id'].

Currently, the way optional values are done is not so good, and relies entirely on regular expressions. You can do it this way at the moment.

<?php
map('/quiz/question/{action}(/{id})?', function ($args) {
  # optional params may not be set in args if not supplied
  $id = isset($args['id']) ? $args['id'] : null;
});

As for query string parameters, you can still get them via $_GET. Dispatch doesn't abstract PHP's super globals, only the route params.