【发布时间】:2014-05-04 20:44:57
【问题描述】:
有人在 Yii 2.0(beta) 中使用集成的 RESTful Web 服务吗?
官方documentation 中的说明看起来很简单,但对我不起作用:
我使用的是基本模板,已经使用 gii 模块创建了一个简单的 'category' 模型扩展 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 被分配给ActiveController 类需要的$modelClass 属性,以将其与已定义的CRUD 操作(如index 或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".
有人知道我做错了什么吗?
谢谢
【问题讨论】: