[英]Yii 2.0 Restful Web Service Api
是否有人在Yii 2.0(测试版)中使用集成的RESTful Web服务?
官方文档中的说明看起来很简单,但对我却不起作用:我使用的是基本模板,使用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';
}
现在,将模型Category
分配给$modelClass
属性, ActiveController
类将需要将其与已经定义的CRUD操作(例如index
或view
:(参见ActiveController :: actions() )相关联。
我的UrlManager
配置看起来像:
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
['class' => 'yii\rest\UrlRule', 'controller' => 'categories'],
],
],
因为我的网络服务器的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! 有用!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.