簡體   English   中英

Yii 2.0 Restful Web Service API

[英]Yii 2.0 Restful Web Service Api

是否有人在Yii 2.0(測試版)中使用集成的RESTful Web服務?

官方文檔中的說明看起來很簡單,但對我卻不起作用:我使用的是基本模板,使用gii模塊創建了擴展ActiveRecord的簡單“類別”模型,然后創建了擴展ActiveControllerCategoriesController

# 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操作(例如indexview :(參見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

PrettyUrlsshowScriptName為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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM