Yii 2.0 Restful Web Service API [英] Yii 2.0 Restful Web Service Api

查看:49
本文介绍了Yii 2.0 Restful Web Service API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

# 文件 app\controllers\CategoriesController.php 的内容<?php命名空间应用\控制器;使用 yii\rest\ActiveController;类 CategoriesController 扩展了 ActiveController{公共 $modelClass = 'app\models\Category';}

现在模型 Category 被分配给 $modelClass 属性,ActiveController 类需要它来将它与已经定义的CRUD 操作,如 indexview:(参见 ActiveController::actions() )

我的 UrlManager 配置如下:

'urlManager' =>['enablePrettyUrl' =>真的,'enableStrictParsing' =>真的,'showScriptName' =>错误的,'规则' =>[['类' =>'yii\rest\UrlRule', '控制器' =>'类别'],],],

因为我的 webserver 的 documentRoot 和我的 webapp 在不同的文件夹中,我在 WEB 文件夹下的 htaccess 文件看起来像:

RewriteEngine onRewriteBase/~salem/alpha2/web# 如果目录或文件存在,直接使用请求RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-d# 否则将请求转发到 index.php重写规则.索引.php

PrettyUrls &showScriptName 到 false 工作正常,但是在尝试访问 localhost/~salem/alpha2/web/categories 时,我遇到以下错误:

未找到 (#404)无法解析请求类别/索引".

有人知道我做错了什么吗??

谢谢

解决方案

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

Anyone using the integrated RESTful Web Service in Yii 2.0(beta)?

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:

# 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() )

My UrlManager configs looks like:

'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:

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:

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! it works!

这篇关于Yii 2.0 Restful Web Service API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆