I have a problem calling the url of my rest api in Yii2. I want to call a url like:
http://localhost/index-dev.php/myapi/collection/18
where 18 is the Id.
In my web.php config, I have the following code, with other settings from another programmers :
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => true,
'rules' => [
['class' => 'yii\rest\UrlRule', 'controller' => ['coding/nodes', 'coding/scales','myapi/collection']],
'<controller:\w+>/<id:\d+>' => '<controller>/view',
],
],
when i call my URL, i get
Not Found (#404)
What am i doing wrong?
I had the same problem, you can disable the prural
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => true,
'rules' => [
['class' => 'yii\rest\UrlRule', 'controller' => ['coding/nodes', 'coding/scales','myapi/collection']],
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'pluralize' => false,
],
],
You need to use the plural of your model class name in the URL for accessing a single model:
http://localhost/index-dev.php/myapi/collections/18
Take a look at the documentation of yii\rest\UrlRule:
The above code will create a whole set of URL rules supporting the following RESTful API endpoints:
- 'PUT,PATCH users/<id>' => 'user/update': update a user
- 'DELETE users/<id>' => 'user/delete': delete a user
- 'GET,HEAD users/<id>' => 'user/view': return the details/overview/options of a user
- 'POST users' => 'user/create': create a new user
- 'GET,HEAD users' => 'user/index': return a list/overview/options of users
- 'users/<id>' => 'user/options': process all unhandled verbs of a user
- 'users' => 'user/options': process all unhandled verbs of user collection
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With