有人在 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 操作相关联,例如index
or 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".
任何人都知道我做错了什么?
谢谢