YII2 Restfull奇怪的行为

时间:2015-08-19 09:32:41

标签: php rest get yii2 restful-architecture

尝试使用Yii2 Advanced模板创建API应用程序(基于RESTFull服务)。为此目的创建了子域api.domain.com。

目录结构如下所示:

enter image description here

问题是,当我尝试向api.domain.com/users发送GET请求以获得以下响应时:

{
  "name": "Not Found",
  "message": "Page not found.",
  "code": 0,
  "status": 404,
  "type": "yii\web\NotFoundHttpException",
  "previous": {
    "name": "Invalid Route",
    "message": "Unable to resolve the request \"user/index\".",
    "code": 0,
    "type": "yii\base\InvalidRouteException"
  }
}

这是配置文件:

https://gist.github.com/d1930b6bf20e3d50fe63

这是控制器(位于Controllers文件夹中):

https://gist.github.com/anonymous/180a7e791e879570e0f4

问题是......

我做错了什么?

2 个答案:

答案 0 :(得分:4)

确保将api应用程序的路径添加到公共文件夹中的bootstrap.php。它必须看起来像:

<?php
Yii::setAlias('common', dirname(__DIR__));
Yii::setAlias('frontend', dirname(dirname(__DIR__)) . '/frontend');
Yii::setAlias('backend', dirname(dirname(__DIR__)) . '/backend');
Yii::setAlias('console', dirname(dirname(__DIR__)) . '/console');
Yii::setAlias('api', dirname(dirname(__DIR__)) . '/api');                    <-- this line

BTW,档案位于common/config/bootstrap.php

答案 1 :(得分:0)

三件事:

  1. 检查复数形式。例如:“ v1 / post”应为“ v1 / posts”
  2. 添加“动作”前缀。例如:“ actionImport()”
  3. 烤肉串格式。例如:“ v1 / getSample”应为“ v1 / get-sample”
  4. 如果需要,在main.php:urlManager中添加规则'extraPatterns'。例如:

    ['class'=>'yii \ rest \ UrlRule','controller'=>'v1 / report','extraPatterns'=> ['analytics'=>'analytics',                                                                                                  'report'=>'report']],