First of all, sorry, if this question sounds really dumb, but being a developer for 25+ years and I still haven’t… developed even an inch of my personal love toward regular expressions. Even more, I feel chills coming down my spin each time I have to deal with them or even think about them.
As I understand that these are regular expressions:
public array $tokens = [
'{id}' => '<id:\d[\d,]*>',
]
Am I correct?
So, my first question is, why Yii 2 is using such way of formatting / providing them, different than pure PHP is using:
$exp = "/w3schools/i";
$pattern = "/ain/i";
Why Yii 2 is using single backslash, if PHP is using single slash?
I have found some other Yii 2 example (the one that I am interested the most – modifying URL patterns for REST app):
'tokens' => [
'{id}' => '<id:\\d[\\d,]*>',
'{type}' => '<type:\\w[\\w,]*>',
'{key}' => '<key:\\w[\\w,]*>',
],
And, as you can see, it uses double backslash instead of single backslash (Yii 2 guide) or single slash (PHP guide). Which only adds to my mystery.
So, second question, what is suggested / preferred way of using delimiters in regular expressions when using them in Yii2? And what is the difference, if any?
Third question or rather doubt is that mentioned example is using \\d
to denote any digit (Yii 2 guide has \d
for this, and W3Schools has it as well. But that Stack Overflow example has \\w
to denote any character, which I can’t find in W3Schools page.
Fourth question is, if there is any specific page in Yii 2 Guide that has exactly this format (<id:\d[\d,]*>
) described for dumb newbies like myself?
The final question, that binds all of them above is how can I modify my existing URLRule configuration to accept dots (.
) in URLs that are executed against my REST app?
Right now I have it coded like that:
'class' => \yii\rest\UrlRule::class,
'controller' => ['device'],
'pluralize' => false,
'extraPatterns' => ['GET,HEAD find/{key}' => 'find'],
'tokens' => [
'{id}' => '<id:\\d[\\d,]*>',
'{key}' => '<key:\\w[\\w,]*>',
],
And the URLs like api/web/device/find/TEST
or api/web/device/find/TEST2
works just like a charm. But in the same time the URL that contains dots (api/web/device/find/TEST.040
) fails with 404.
How can I add support for dots to my URL?