Daaaaa
Daaaaa

Reputation: 57

Make minus sign optional in Regex (AltoRouter 1.2.0)

I'm using AltoRouter 1.2.0 as a PHP router.

I'd like to use an optional parameter ('page'), and make a minus sign optional in the URL.

If page number is 2, URL should contain the thread ID and the page number, separated by a minus sign.

/forum/24981-2/my-super-forum-thread

If page number is one, the page parameter shouldn't be in the URL, and URL should be something like this

/forum/24981/my-super-forum-thread (no page number, no minus sign)

AltoRouter's optional parameters depend on this Regex, as you can see in the doc :

''   => '[^/\.]++'

It makes the slash and the dot optional in the URL, it works as it should. I'd like to make the minus sign optional as well.

My route declaration is currently like this

$router->map('GET', '/forum/[i:thread_id]-[:page]?/[s:slug]', 'forum#getThread', 'forum_getThread');

I've tried a lot of different regex, like adding the minus sign at the beginning or end of the brackets, but no luck.

Any help is appreciated, i've checked a lot of questions on SO regarding the minus sign in regex, but couldn't make it. Thanks in advance for any tip.

Upvotes: 0

Views: 128

Answers (0)

Related Questions