I've got a simple Yii2 application set up following this Quick Start Guide . It's so generic and there's no extra code in it. But for some reason my CURL request returns an extra <?PHP
tag that's messing it all up.
My Request:
curl -i -H "Accept:application/json" "http://backend/users"
The Response:
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
My main.php
config file looks like this:
<?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,
];
My UserController.php
file looks like this:
<?php
namespace backend\controllers;
use yii\rest\ActiveController;
class UserController extends ActiveController
{
public $modelClass = 'common\models\User';
}
And my User.php
model file looks like this:
<?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
I'd go ahead and search your project folder for <php
.
$ grep -r '<php' /path/to/app
If that doesn't help, you could try modifying your index.php file to find where headers are sent , but this won't work if headers are sent by the framework before the output begins:
if (headers_sent($filename, $linenum)) {
echo "Headers sent in $filename on line $linenum";
}
After following their guide rather quickly, I was able to get this:
$ 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"}]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.