【问题标题】:Yii2 REST Example Returns Errant <?PHP TagYii2 REST 示例返回错误 <?PHP 标记
【发布时间】:2015-01-28 16:06:37
【问题描述】:

我在Quick Start Guide 之后设置了一个简单的 Yii2 应用程序。它是如此通用,并且其中没有额外的代码。但由于某种原因,我的 CURL 请求返​​回了一个额外的 &lt;?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 rest curl yii2


    【解决方案1】:

    很抱歉打扰大家...原来在我的 config/bootstrap.php 文件中,我的 IDE 在打开 PHP 标记后修剪了空格,所以我有“

    【讨论】:

      【解决方案2】:

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

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-07-27
        • 2016-07-17
        • 2013-12-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多