1

有人在 Yii 2.0(beta) 中使用集成的 RESTful Web 服务吗?

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

# 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 操作相关联,例如indexor view:( 请参阅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".

任何人都知道我做错了什么?

谢谢

4

1 回答 1

0

答案在上面链接的相同文档中:“......当创建一个新的控制器类时,命名控制器类的约定是使用资源的类型名称并使用单数形式。例如,提供用户信息,控制器可能被命名为 UserController...” 我禁用了 enableStrictParsing,删除了规则,然后现在将我的控制器重命名为 CategoryController!有用!

于 2015-03-12T14:58:40.520 回答