Sunday 15 May 2011

rest - How do I solve this Yii2 RESTful Web Service API routing error? -



rest - How do I solve this Yii2 RESTful Web Service API routing error? -

i trying implement yii2 restful web service api described in guide. trying using advanced application template. problem 404 errors when seek access service. wanted start out trying simple going utilize country table , associated activerecord class seek out, here's code:

this in components configuration in frontend/config/main.php :

'urlmanager' => [ 'enableprettyurl' => true, 'enablestrictparsing' => true, 'showscriptname' => false, 'rules' => [ ['class' => 'yii\rest\urlrule', 'controller' => 'country'], ], ], 'request' => [ 'parsers' => [ 'application/json' => 'yii\web\jsonparser', ] ]

this code in frontend/controllers/countrycontroller.php :

namespace frontend\controllers; utilize yii\rest\activecontroller; class countrycontroller extends activecontroller { public $modelclass = 'common\models\country'; }

all of activerecord models country in common/models.

i used next seek it:

curl -i -h "accept:application/json" "http://myfrontendapp.loc/country"

this output get:

http/1.1 404 not found date: fri, 31 oct 2014 22:46:50 gmt server: apache content-length: 205 content-type: text/html; charset=iso-8859-1 <!doctype html public "-//ietf//dtd html 2.0//en"> <html><head> <title>404 not found</title> </head><body> <h1>not found</h1> <p>the requested url /country not found on server.</p> </body></html>

i have spent many hours on this. have adjusted setting, read documentation, , alot more without success. if can see problem please allow me know, thanks!!

you're nail default urlrule behavior of pluralizing class names: in line mutual rest conventions, it's smart plenty turn controller named country pluralized route named /countries.

try get http://myfrontendapp.loc/countries , it'll work fine. goes other routes created urlrule, e.g. get /countries/12345

or, if want disable behavior can set urlrule's $pluralize false. check http://www.yiiframework.com/doc-2.0/guide-rest-routing.html more information.

rest yii2

No comments:

Post a Comment