2

I am trying to implement the Yii2 RESTful Web Service API as described in the guide. I am trying it using the advanced application template. The problem is simply that all I get are 404 errors when I try to access the service. I wanted to start out trying something simple so I was simply going to use a country table and associated ActiveRecord class to try it out, here's the code:

This is in the 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 is the code in frontend/controllers/CountryController.php :

namespace frontend\controllers; use yii\rest\ActiveController; class CountryController extends ActiveController { public $modelClass = 'common\models\Country'; } 

All of my ActiveRecord models like country are in common/models.

I used the following to try it:

curl -i -H "Accept:application/json" "http://myfrontendapp.loc/country" 

This is the output I 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 was not found on this server.</p> </body></html> 

I have spent many hours on this. I have adjusted the setting, read documentation, and alot more without any success. If anyone can see what the problem is please let me know, thanks!!

1
  • I tried it with the basic application template and had no problems whatsoever. If anyone figures out how to do this using the advanced application template let me know. Commented Nov 7, 2014 at 19:05

4 Answers 4

5

You're hit by the default UrlRule behavior of pluralizing class names: in line with common REST conventions, it's smart enough to turn a controller named country into a pluralized route named /countries.

Try to GET http://myfrontendapp.loc/countries and it'll most likely work fine. This also goes for all other routes created by UrlRule, e.g. GET /countries/12345

Or, if you want to disable this behavior you can set UrlRule's $pluralize to false. Check http://www.yiiframework.com/doc-2.0/guide-rest-routing.html for more information.

Sign up to request clarification or add additional context in comments.

1 Comment

I noticed that in the docs and I tried it, both "countries" and "countrys" with the exact same result, no luck. Thanks for the suggestion!
1

According to yii2 rest standards while running your application you have to very specific with your URI i.e. as you are running the application for country so while running pluralize it so while running you should run as http://myfrontendapp.loc/countries. But still you want country only then in your main.php under your urlmanager type 'controller'=>['c','country']. And if your problem still persists then you need to check your dir structure... It should be yii dir api, backend, frontend under api their should be config, modules inside moudles --v1 and inside v1-- countrollers and models. etc.
This will surely resolve your issue...

Comments

0

Try this:

  • In advanced/api/web/index.html add require(__DIR__ . '/../../common/config/bootstrap.php'); instead of require(__DIR__ . '/../../common/config/aliases.php');

  • In advanced/common/config/bootstrap.php add Yii::setAlias('api', dirname(dirname(__DIR__)) . '/api');

  • Look through this tutorial

And make sure your GET request is correct. Remove 'api/web' from reqesst line. Just use v1/countrys

Comments

0

Have you placed

.htaccess

in the folder, if not create the

.htaccess

with the following content

RewriteEngine on # If a directory or a file exists, use the request directly RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # Otherwise forward the request to index.php RewriteRule . index.php 

Or you can check the request with the following url once, as you are using advanced templaet

curl -i -H "Accept:application/json" "http://myfrontendapp.loc/frontend/web/index.php?r=countries" 

One more thing to remember folder names are also case-sensitive while declaring namespaces, so please check the folder names as well.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.