REST api 不工作
REST api not working
我是 php
开发的新手。只是为了练习,我正在按照视频教程创建休息 API。我已经按照每个步骤进行操作,但仍然无法获得所需的结果。下面是代码
员工模型
class Employee extends \yii\db\ActiveRecord
{
const SCENARIO_CREATE = 'create';
/**
* @inheritdoc
*/
public static function tableName()
{
return 'employee';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['emp_name', 'emp_email', 'emp_sal'], 'required'],
[['emp_name', 'emp_email', 'emp_sal'], 'string', 'max' => 100],
];
}
public function scenarios()
{
$scenarios = parent::scenarios();
$scenarios['create'] = ['emp_name','emp_email', 'emp_sal'];
return $scenarios;
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'emp_name' => 'Emp Name',
'emp_email' => 'Emp Email',
'emp_sal' => 'Emp Sal',
];
}
}
ID
字段上方是 auto-increment
员工控制器
public function actionCreateEmployee()
{
\Yii::$app->response->format= \yii\web\Response::FORMAT_JSON;
$employee = new Employee();
$employee-> scenario = Employee::SCENARIO_CREATE;
$employee->attributes = \Yii::$app->request->post();
if ($employee->validate())
{
return array('status'=> true, 'data' => 'Employee Created Sussessfully');
}
else
{
return array('status'=> false, 'data'=>$employee->getErrors());
}
//return array('status'=> true);
}
现在当我 运行 Postman
中的 API 时。我得到了以下结果。
虽然我已经输入了所有必填字段数据,但它仍然给我 false status
非常感谢任何帮助
你需要selectx-www-form-urlencoded
文档说 $_POST
参数只在 application/x-www-form-urlencoded
或 multipart/form-data
上填充,yii 可能正在使用它。
An associative array of variables passed to the current script via the HTTP POST method when using application/x-www-form-urlencoded or multipart/form-data as the HTTP Content-Type in the request.
我是 php
开发的新手。只是为了练习,我正在按照视频教程创建休息 API。我已经按照每个步骤进行操作,但仍然无法获得所需的结果。下面是代码
员工模型
class Employee extends \yii\db\ActiveRecord
{
const SCENARIO_CREATE = 'create';
/**
* @inheritdoc
*/
public static function tableName()
{
return 'employee';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['emp_name', 'emp_email', 'emp_sal'], 'required'],
[['emp_name', 'emp_email', 'emp_sal'], 'string', 'max' => 100],
];
}
public function scenarios()
{
$scenarios = parent::scenarios();
$scenarios['create'] = ['emp_name','emp_email', 'emp_sal'];
return $scenarios;
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'emp_name' => 'Emp Name',
'emp_email' => 'Emp Email',
'emp_sal' => 'Emp Sal',
];
}
}
ID
字段上方是 auto-increment
员工控制器
public function actionCreateEmployee()
{
\Yii::$app->response->format= \yii\web\Response::FORMAT_JSON;
$employee = new Employee();
$employee-> scenario = Employee::SCENARIO_CREATE;
$employee->attributes = \Yii::$app->request->post();
if ($employee->validate())
{
return array('status'=> true, 'data' => 'Employee Created Sussessfully');
}
else
{
return array('status'=> false, 'data'=>$employee->getErrors());
}
//return array('status'=> true);
}
现在当我 运行 Postman
中的 API 时。我得到了以下结果。
虽然我已经输入了所有必填字段数据,但它仍然给我 false status
非常感谢任何帮助
你需要selectx-www-form-urlencoded
文档说 $_POST
参数只在 application/x-www-form-urlencoded
或 multipart/form-data
上填充,yii 可能正在使用它。
An associative array of variables passed to the current script via the HTTP POST method when using application/x-www-form-urlencoded or multipart/form-data as the HTTP Content-Type in the request.