我试图按照指南中所述实现Yii2 RESTful Web服务API。我正在尝试使用高级应用程序模板。问题很简单,当我尝试访问服务时,我得到的所有404错误都是错误的。我想开始尝试一些简单的东西,所以我只是想使用国家表和相关的ActiveRecord类来试用它,代码如下:如何解决Yii2 RESTful Web服务API路由错误?
这是在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',
]
]
这是在前端/控制器代码/ CountryController.php:
我的ActiveRecord模型,如国家的namespace frontend\controllers;
use yii\rest\ActiveController;
class CountryController extends ActiveController
{
public $modelClass = 'common\models\Country';
}
全部都是常见/模型。
我用下面来试试吧:
curl -i -H "Accept:application/json" "http://myfrontendapp.loc/country"
这是输出我得到:
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>
我已经在这个花了很多时间。我已经调整了设置,阅读文档,并且没有任何成功。如果有人可以看到问题是什么,请让我知道,谢谢!
我尝试了基本的应用程序模板,并没有任何问题。如果有人知道如何使用高级应用程序模板来做到这一点,请告诉我。 – dataskills 2014-11-07 19:05:16