Yii2 REST 示例 Returns 错误 <?PHP 标记

Yii2 REST Example Returns Errant <?PHP Tag

我已经按照此设置了一个简单的 Yii2 应用程序 Quick Start Guide。它是如此通用,其中没有额外的代码。但出于某种原因,我的 CURL 请求 returns 一个额外的 <?PHP 标签,这把一切都搞砸了。

我的要求:

curl -i -H "Accept:application/json" "http://backend/users"

响应:

HTTP/1.1 200 OK
Date: Wed, 28 Jan 2015 15:55:14 GMT
Server: Apache/2.2.27 (Unix) mod_ssl/2.2.27 OpenSSL/1.0.1j DAV/2 PHP/5.5.16
X-Powered-By: PHP/5.5.16
X-Pagination-Total-Count: 1
X-Pagination-Page-Count: 1
X-Pagination-Current-Page: 1
X-Pagination-Per-Page: 20
Link: <http://backend/users?page=1>; rel=self
Content-Length: 178
Content-Type: application/json; charset=UTF-8

<?php[{"id":1,"email":"chris@email.com","password":"","name":null,"address":null,"address2":null,"city":null,"state":null,"zip":null,"date_created":null,"date_updated":null}]

TL;DR

我的 main.php 配置文件如下所示:

<?php
$params = array_merge(
    require(__DIR__ . '/../../common/config/params.php'),
    require(__DIR__ . '/../../common/config/params-local.php'),
    require(__DIR__ . '/params.php'),
    require(__DIR__ . '/params-local.php')
);

return [
    'id' => 'app-backend',
    'basePath' => dirname(__DIR__),
    'controllerNamespace' => 'api\controllers',
    'bootstrap' => ['log'],
    'modules' => [],
    'components' => [
        'user' => [
            'identityClass' => 'common\models\User',
            'enableAutoLogin' => true,
        ],
        'urlManager' => [
            'enablePrettyUrl' => true,
            'enableStrictParsing' => true,
            'showScriptName' => false,
            'rules' => [
                ['class' => 'yii\rest\UrlRule', 'controller' => 'user'],
            ],
        ],
        'request' => [
            'parsers' => [
                'application/json' => 'yii\web\JsonParser',
            ]
        ]
    ],
    'params' => $params,
];

我的 UserController.php 文件如下所示:

<?php

namespace backend\controllers;

use yii\rest\ActiveController;

class UserController extends ActiveController
{
    public $modelClass = 'common\models\User';
}

我的 User.php 模型文件如下所示:

<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "users".
 *
 * @property integer $id
 * @property string $email
 * @property string $password
 * @property string $name
 * @property string $address
 * @property string $address2
 * @property string $city
 * @property string $state
 * @property string $zip
 * @property string $date_created
 * @property string $date_updated
 */
class User extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'users';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['date_created', 'date_updated'], 'safe'],
            [['email', 'password'], 'string', 'max' => 255],
            [['name', 'address', 'address2'], 'string', 'max' => 100],
            [['city'], 'string', 'max' => 50],
            [['state'], 'string', 'max' => 2],
            [['zip'], 'string', 'max' => 10]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'email' => 'Email',
            'password' => 'Password',
            'name' => 'Name',
            'address' => 'Address',
            'address2' => 'Address2',
            'city' => 'City',
            'state' => 'State',
            'zip' => 'Zip',
            'date_created' => 'Date Created',
            'date_updated' => 'Date Updated',
        ];
    }
}

我会继续在您的项目文件夹中搜索 <php

$ grep -r '<php' /path/to/app

如果这没有帮助,您可以尝试修改您的 index.php 文件以查找 headers are sent 的位置,但如果 headers 之前由框架发送,这将不起作用输出开始:

if (headers_sent($filename, $linenum)) {
    echo "Headers sent in $filename on line $linenum";
}

在相当快地按照他们的指南进行操作后,我得到了这个:

$ curl -i -H "Accept:application/json" "http://localhost/users"

HTTP/1.1 200 OK
Date: Wed, 28 Jan 2015 19:47:03 GMT
Server: Apache
X-Powered-By: PHP/5.5.14
X-Pagination-Total-Count: 1
X-Pagination-Page-Count: 1
X-Pagination-Current-Page: 1
X-Pagination-Per-Page: 20
Link: <http://localhost/users?page=1>; rel=self
Content-Length: 94
Content-Type: application/json; charset=UTF-8

[{"id":1,"created":"2015-01-28 00:00:00","modified":"2015-01-28 00:00:00","name":"Test User"}]

抱歉打扰大家...结果在我的 config/bootstrap.php 文件中,我的 IDE 修剪了开始 PHP 标签后的空白,而不是 "