A
A
Antago2015-01-07 19:13:12
Yii
Antago, 2015-01-07 19:13:12

How to work with Yii2 REST API when post id is of varchar type?

If the record id has a type other than integer, then the Rest API in Yii2 starts returning 404 when receiving information on a single record. Did someone come across? How is it decided?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
T
Terminaft, 2015-01-07
@Terminaft

Show URLManager'a config. Spherical advice - put another regular expression in 'tokens', or use 'extraPatterns'.
UPD:
Based on your comment, the config should be like this

'rules' => [
   [
      'class' => 'yii\rest\UrlRule',
      'controller' => 'products',
      'tokens' => [
         '{id}' => '<id:\w+>'
      ],
   ],
]

A
Antago, 2015-01-07
@Antago

Another feature is that 404 returns only if the UrlManager is written universally:
['class' => 'yii\rest\UrlRule', 'controller' => 'products'],
but if the rules are set manually, then everything is fine :
'products/' => '/product/view'
Accordingly, the problem is in UrlManager, which for REST means id is only a number. Solved by replacing $tokens regular expression in yii\rest\UrlRule

R
Roman, 2017-06-13
@ameba0

$html = file_get_contents('index.html');
$i = 0;
$html = preg_replace_callback(
  '/<p/i',
  function() {
    global $i;
    $i++;

    return "<p data-name=\"{$i}\"";
  },
  $html
);

file_put_contents('index-out.html', $html);

// Finished in 0.1s
Checked on the text with 11K tags from the text of the 1st volume of "War and Peace" az.lib.ru/t/tolstoj_lew_nikolaewich/text_0040.shtml<dd>

Николай Конюхов, 2017-06-13
@heahoh

Не использовать preg_replace

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question