Answer the question
In order to leave comments, you need to log in
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
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+>'
],
],
]
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
$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);
<dd>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question