สร้างเว็บ api restful ด้วย yii2
1 สร้าง table ในการเก็บข้อมูล
CREATE TABLE `student` (
`ID` int(11) NOT NULL PRIMARY KEY,
`Name` varchar(40) NOT NULL,
`Last_Name` varchar(40) NOT NULL,
`Email` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
2 สร้าง model อิง table นั้น (ใช้ gii จะง่ายและเร็วมาก)
<?php
namespace app\models;
use Yii;
class Student extends \yii\db\ActiveRecord
{
const SCENARIO_CREATE = 'create';
public static function tableName()
{
return 'student';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['ID', 'Name', 'Last_Name', 'Email'], 'required'],
[['ID'], 'integer'],
[['Name', 'Last_Name'], 'string', 'max' => 40],
[['Email'], 'string', 'max' => 255],
[['ID'], 'unique'],
];
}
public function scenarios()
{
$scenarios = parent::scenarios();
$scenarios['create'] = ['Name','Last_Name','Email'];
return $scenarios;
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'ID' => 'ID',
'Name' => 'Name',
'Last_Name' => 'Last Name',
'Email' => 'Email',
];
}
}
3 สร้าง controller
<?php
namespace app\controllers;
use Yii;
use app\models\Student;
class StudentController extends \yii\web\Controller
{
public $enableCsrfValidation = false;
public function beforeAction($action)
{
$this->enableCsrfValidation = false;
if (parent::beforeAction($action)) {
if (in_array($action->id, array('create', 'update','put','post','delete'))) {
}
return true;
} else {
return false;
}
}
public function actionIndex()
{
// return $this->render('index');
return "ok";
}
public function actionCreateStudent(){
\Yii::$app->response->format = \yii\web\Response:: FORMAT_JSON;
$student = new Student();
$student->scenario = Student:: SCENARIO_CREATE;
$student->attributes = \yii::$app->request->post();
if($student->validate()){
$student->save();
return array('status' => true, 'data'=> 'Student record is successfully updated');
}
else{
return array('status'=>false,'data'=>$student->getErrors());
}
}
public function actionGetStudent()
{
\Yii::$app->response->format = \yii\web\Response:: FORMAT_JSON;
$student = Student::find()->all();
if(count($student) > 0 )
{
return array('status' => true, 'data'=> $student);
}
else
{
return array('status'=>false,'data'=> 'No Student Found');
}
}
public function actionUpdateStudent(){
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
$attributes = \yii::$app->request->post();
$student = Student::find()->where(['ID' => $attributes['id'] ])->one();
if(count($student) > 0 ){
$student->attributes = \yii::$app->request->post();
$student->save();
return array('status' => true, 'data'=> 'Student record is updated successfully');
}
else{
return array('status'=>false,'data'=> 'No Student Found');
}
}
public function actionDeleteStudent(){
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
$attributes = \yii::$app->request->post();
$student = Student::find()->where(['ID' => $attributes['id'] ])->one();
if(count($student) > 0 ){
$student->delete();
return array('status' => true, 'data'=> 'Student record is successfully deleted');
}
else{
return array('status'=>false,'data'=> 'No Student Found');
}
}
}
ไม่มีความคิดเห็น:
แสดงความคิดเห็น