正在设置未知属性:yiiwebUrlRule::GET



我有这个错误,我不知道该怎么解决。

我用Yii2创建了一个api端点。几天前,端点还在工作,但现在不工作了,并引发了以下错误:

{
name: "Unknown Property",
message: "Setting unknown property: yiiwebUrlRule::GET index",
code: 0,
type: "yiibaseUnknownPropertyException",
file: "C:xampphtdocsforkandjoinvendoryiisoftyii2baseObject.php",
line: 161,
stack-trace: [
"#0 C:xampphtdocsforkandjoinvendoryiisoftyii2BaseYii.php(521): yiibaseObject->__set('GET index', 'index')",
"#1 C:xampphtdocsforkandjoinvendoryiisoftyii2baseObject.php(105): yiiBaseYii::configure(Object(yiiwebUrlRule), Array)",
"#2 [internal function]: yiibaseObject->__construct(Array)",
"#3 C:xampphtdocsforkandjoinvendoryiisoftyii2diContainer.php(374): ReflectionClass->newInstanceArgs(Array)",
"#4 C:xampphtdocsforkandjoinvendoryiisoftyii2diContainer.php(153): yiidiContainer->build('yii\web\UrlRule', Array, Array)",
"#5 C:xampphtdocsforkandjoinvendoryiisoftyii2BaseYii.php(344): yiidiContainer->get('yii\web\UrlRule', Array, Array)",
"#6 C:xampphtdocsforkandjoinvendoryiisoftyii2webUrlManager.php(212): yiiBaseYii::createObject(Array)",
"#7 C:xampphtdocsforkandjoinvendoryiisoftyii2webUrlManager.php(154): yiiwebUrlManager->buildRules(Array)",
"#8 C:xampphtdocsforkandjoinvendoryiisoftyii2baseObject.php(107): yiiwebUrlManager->init()",
"#9 [internal function]: yiibaseObject->__construct(Array)",
"#10 C:xampphtdocsforkandjoinvendoryiisoftyii2diContainer.php(374): ReflectionClass->newInstanceArgs(Array)",
"#11 C:xampphtdocsforkandjoinvendoryiisoftyii2diContainer.php(153): yiidiContainer->build('yii\web\UrlMana...', Array, Array)",
"#12 C:xampphtdocsforkandjoinvendoryiisoftyii2BaseYii.php(344): yiidiContainer->get('yii\web\UrlMana...', Array, Array)",
"#13 C:xampphtdocsforkandjoinvendoryiisoftyii2diServiceLocator.php(133): yiiBaseYii::createObject(Array)",
"#14 C:xampphtdocsforkandjoinvendoryiisoftyii2baseApplication.php(563): yiidiServiceLocator->get('urlManager')",
"#15 C:xampphtdocsforkandjoinvendoryiisoftyii2webRequest.php(180): yiibaseApplication->getUrlManager()",
"#16 C:xampphtdocsforkandjoinvendoryiisoftyii2webApplication.php(75): yiiwebRequest->resolve()",
"#17 C:xampphtdocsforkandjoinvendoryiisoftyii2baseApplication.php(375): yiiwebApplication->handleRequest(Object(yiiwebRequest))",
"#18 C:xampphtdocsforkandjoinapiindex.php(13): yiibaseApplication->run()",
"#19 {

这是我的urlManager配置:

modules' => [
        'v1' => [
            'basePath' => '@app/api/modules/v1', // base path for our module class
            'class' => 'appapimodulesv1Api', // Path to module class
        ]
    ]
'components'  => [
        'urlManager'  => [
            'enablePrettyUrl'  => true,
            'showScriptName'  => false,
            'enableStrictParsing' => false,
            'rules' => [
                'class'  => 'yiirestUrlRule',
                'controller'  => 'v1/demo',
                'patterns' => [
                    'GET index' => 'index'
                ]
            ],
        ],

我用一个Demo控制器和一个简单的索引端点创建了这个简单的例子,以便更清楚地了解这个问题。

我该如何解决这个问题?为什么返回未知属性。

信息

OS: Windows 10.
Composer: version 1.0.0 2016-04-05 13:27:25
Yii version: 2

我的composer文件

{
    "name": "yiisoft/yii2-app-advanced",
    "description": "Yii 2 Advanced Project Template",
    "keywords": ["yii2", "framework", "advanced", "project template"],
    "homepage": "http://www.yiiframework.com/",
    "type": "project",
    "license": "BSD-3-Clause",
    "support": {
        "issues": "https://github.com/yiisoft/yii2/issues?state=open",
        "forum": "http://www.yiiframework.com/forum/",
        "wiki": "http://www.yiiframework.com/wiki/",
        "irc": "irc://irc.freenode.net/yii",
        "source": "https://github.com/yiisoft/yii2"
    },
    "minimum-stability": "stable",
    "require": {
        "php": ">=5.4.0",
        "yiisoft/yii2": ">=2.0.6",
        "yiisoft/yii2-bootstrap": "*",
        "yiisoft/yii2-swiftmailer": "*",
        "linslin/yii2-curl": "*"
    },
    "require-dev": {
        "yiisoft/yii2-codeception": "*",
        "yiisoft/yii2-debug": "*",
        "yiisoft/yii2-gii": "*",
        "yiisoft/yii2-faker": "*"
    },
    "config": {
        "process-timeout": 1800
    },
    "extra": {
        "asset-installer-paths": {
            "npm-asset-library": "vendor/npm",
            "bower-asset-library": "vendor/bower"
        }
    }
}

如果你需要更多信息,请告诉我,但我现在需要解决这个问题,否则我将成为一个没有回报的疯子。

更新:控制器

这是我的控制器:

<?php
namespace appapimodulesv1controllers;
use yiirestController;
class DemoController extends Controller
{
    public function behaviors() {
        return [
            [
                'class' => yiifiltersContentNegotiator::className(),
                'formats' => [
                    'application/json' => yiiwebResponse::FORMAT_JSON,
                ],
            ],
        ];
    }
    public function actionIndex(){
        return "index ";
    }
}

Yii2路由指南

我按照关于路由的官方指南进行了这些配置:

[
    'class' => 'yiirestUrlRule',
    'controller' => 'user',
    'extraPatterns' => [
        'GET search' => 'search',
    ],
]

哇,谢谢大家。我整个周末都在努力解决这个问题,根据您的反馈和研究,我发现配置中有一个小错误。在Yii2指南中,用两段代码解释配置。所以我不得不更改我的代码:

'rules' => [
    'class'  => 'yiirestUrlRule',
    'controller'  => 'v1/demo',
    'extraPatterns' => [
        'GET index' => 'index'
   ]
]

这个:

'rules' => [
    [ // array item
      'class'  => 'yiirestUrlRule',
      'controller'  => 'v1/demo',
      'extraPatterns' => [
          'GET index' => 'index'
       ] 
    ]//close the array item
]

好吧,错误已经消失了。

Setting unknown property: yiiwebUrlRule::GET index表示您使用的UrlRule类不正确。它应该是yii\rest\UrlRule。检查组件的配置以确保使用的是正确的配置。

根据文档,它应该是:

        'rules' => [
            'class'  => 'yiirestUrlRule',
            'controller'  => 'v1/demo',
            'patterns' => [
                'GET' => 'index' // removed ' index' in key
            ]
        ],

如果这能解决你的问题,我想知道为什么它几天后才起作用。也许这条路还没叫上?

相关内容

  • 没有找到相关文章

最新更新