Yii 2.0 Restful Web Service Api


Yii 2.0 Restful Web Service Api

有人在使用Yii 2.0(测试版)中的集成rest式Web服务吗?

官方文档中的说明看起来很简单,但对我来说不起作用:我使用基本模板,使用gii模块创建一个简单的'类别'模型扩展ActiveRecord,然后我创建了扩展ActiveControllerCategoriesController:

# Content of the file app'controllers'CategoriesController.php
<?php
namespace app'controllers;
use yii'rest'ActiveController;
class CategoriesController extends ActiveController
{
    public $modelClass = 'app'models'Category';
}

现在模型Category被分配给$modelClass属性,ActiveController类需要将其与已经定义的CRUD操作(如indexview)关联起来:参见ActiveController::actions())

我的UrlManager配置看起来像:

'urlManager' => [
            'enablePrettyUrl' => true,
            'enableStrictParsing' => true,
            'showScriptName' => false,
            'rules' => [
                ['class' => 'yii'rest'UrlRule', 'controller' => 'categories'],
            ],
        ],

因为我的webserver的documentRoot和我的webapp在不同的文件夹中,我的WEB文件夹下的htaccess文件看起来像:

RewriteEngine on
RewriteBase /~salem/alpha2/web
# 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

PrettyUrls &showScriptName到false只是工作正常,但当试图访问localhost/~salem/alpha2/web/categories我有以下错误:

Not Found (#404)
Unable to resolve the request "categories/index".
有谁知道我做错了什么吗?

谢谢

答案在上面链接的相同文档中:"…在创建新的控制器类时,命名控制器类的约定是使用资源的类型名并使用单数形式。例如,为了提供用户信息,控制器可以被命名为UserController…"我禁用了enableStrictParsing,删除了Rules,然后现在将我的控制器重命名为CategoryController !它的工作原理!