0

I'm building an api using the micro framework.

I have the following routes:

/users/{user_id}
/users/{user_id}/friends
/users/{user_id}/pictures

Whats the cleanest way to create the following aliases?

/me
/me/friends
/me/pictures
Boedy
  • 6,647
  • 1
  • 21
  • 24

1 Answers1

1

Phalcon router supports regular expressions, but they are slightly limited, so I'm not 100% certain this would work. Also the way routing works for micro apps might differ, but you can try using the regular expression like this. Make sure $userId defaults to null – if it's null, then you know that it's the current user.

$app->get('(?:/users/(\\w+)|/me)', function($userId = null){});

If this doesn't work then taking out the handler into a non-anonymous function and specifying two routes would be the only way. You can also use micro collections that allow the grouping of handlers.

$app->get('/me', 'userView');
$app->get('/users/{user_id}', 'userView');
Ian Bytchek
  • 8,804
  • 6
  • 46
  • 72