簡體   English   中英

Yii2 REST示例返回錯誤

[英]Yii2 REST Example Returns Errant <?PHP Tag

按照此快速入門指南,我已經建立了一個簡單的Yii2應用程序。 它是如此的通用,其中沒有多余的代碼。 但是由於某種原因,我的CURL請求返回了一個額外的<?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',
        ];
    }
}

抱歉打擾大家...結果在我的config / bootstrap.php文件中,我的IDE在打開PHP標記后修剪了空格,所以我使用的是“ <?php”而不是“ <?php”,它以字符串形式返回,而不是解析為PHP

我繼續在您的項目文件夾中搜索<php

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

如果這樣做沒有幫助,您可以嘗試修改index.php文件以查找將標頭發送到的位置 ,但是如果框架在輸出開始之前發送標頭,則此操作將無效:

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"}]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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