[英]Yii 2.0 Restful Web Service Api
Anyone using the integrated RESTful Web Service in Yii 2.0(beta)? 是否有人在Yii 2.0(测试版)中使用集成的RESTful Web服务?
The instructions in the official documentation looks simple but it didn't work for me: I'm using the basic template, have used the gii module to create a simple 'category' model extending ActiveRecord
, then i created the CategoriesController
extending the ActiveController
: 官方文档中的说明看起来很简单,但对我却不起作用:我使用的是基本模板,使用gii模块创建了扩展ActiveRecord
的简单“类别”模型,然后创建了扩展ActiveController
的CategoriesController
:
# 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';
}
Now the model Category
is assigned to the $modelClass
property which will be needed by the ActiveController
Class to relate it with the already defined CRUD actions like index
or view
:( see ActiveController::actions() ) 现在,将模型Category
分配给$modelClass
属性, ActiveController
类将需要将其与已经定义的CRUD操作(例如index
或view
:(参见ActiveController :: actions() )相关联。
My UrlManager
configs looks like: 我的UrlManager
配置看起来像:
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
['class' => 'yii\rest\UrlRule', 'controller' => 'categories'],
],
],
Because my webserver's documentRoot and my webapp are in separate folders, my htaccess file under the WEB folder looks like: 因为我的网络服务器的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
to false are just working fine, but when trying to access to localhost/~salem/alpha2/web/categories i'm having the following error: PrettyUrls
和showScriptName
为false都可以正常工作,但是当尝试访问localhost /〜salem / alpha2 / web / categories时 ,出现以下错误:
Not Found (#404)
Unable to resolve the request "categories/index".
Any one have any idea about what i'm doing wrong?? 有人对我做错了吗?
Thanks 谢谢
The answer was in the same docs linked above: "...When creating a new controller class, a convention in naming the controller class is to use the type name of the resource and use singular form. For example, to serve user information, the controller may be named as UserController..." i desabled the enableStrictParsing, removed Rules, then renamed my controller to CategoryController now! 答案是在上面链接的同一文档中:“ ...创建新的控制器类时,命名控制器类的约定是使用资源的类型名称并使用单数形式。例如,为了提供用户信息,控制器可以命名为UserController ...”我禁用了enableStrictParsing,删除了Rules,然后将我的控制器重命名为CategoryController! it works! 有用!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.