Yii框架实现restful 接口调用,增删改查

时间:2021-04-26 06:21:33

创建模块modules;

在main.php中配置文件:(1)

Yii框架实现restful 接口调用,增删改查

Yii框架实现restful 接口调用,增删改查

(2)控制器层:

namespace frontend\modules\v1\controllers;
use frontend\modules\v1\models\Fruit;
use yii\rest\ActiveController;
class FruitController extends ActiveController{
protected $result=array(
'code'=>0,
'data'=>'',
'error'=>'',
);
public $modelClass = 'frontend\modules\v1\models\Fruit';
public function actions(){
$actions = parent::actions();
unset($actions['index'], $actions['update'], $actions['create'], $actions['delete'], $actions['view']);
return $actions;
}
//查询单条 get
public function actionView(){
$id=\Yii::$app->request->get('id');
$FruitList=Fruit::get($id);
$this->result['data']=$FruitList;
return $this->result;
}
//查询全部 get
public function actionIndex(){
$FruilList=Fruit::getAll();
$this->result['data']=$FruilList;
return $this->result;
}
//添加 post
public function actionCreate(){
$name=\Yii::$app->request->post('name');
$num=\Yii::$app->request->post('num');
if(empty($name)){
$this->result['code']=1000;
return $this->result;
}if(empty($num)){
$this->result['code']=1001;
return $this->result;
}
$fruit=array(
'name'=>$name,
'num'=>$num
);
if (false === Fruit::add($fruit)){
$this->result['code']=1002;
$this->result['error']='add is error';
}
return $this->result;
} //修改 post
public function actionUpdate(){
$id=\Yii::$app->request->post('id');
$num=\Yii::$app->request->post('num');
if($id<1){
$this->result['code']=1001;
return $this->result;
}
if($num<1){
$this->result['code']=1002;
return $this->result;
}
$params=array(
'num'=>$num,
);
if (false === Fruit::modify($id,$params)){
$this->result['code']=1012;
$this->result['error']='update is error';
}
}
//删除 get
public function actionDelete(){
$id=\Yii::$app->request->get('id');
if($id<1){
$this->result['code']=1020;
return $this->result;
}
if (false === Fruit::del($id)){
$this->result['code']=1022;
$this->result['error']='delete is error';
}
return $this->result;
}
(3)模型层
<?php
namespace frontend\modules\v1\models;
use Yii;
class Fruit extends \yii\base\Model{
private static $_tbl = 'fruit';
//查询所有
public static function getAll(){
$db = Yii::$app->db->createCommand('SELECT `id`,`name`,`num` FROM ' . self::$_tbl);
$result = $db->queryAll();
if( is_array($result) ){
return $result;
}
return array();
}
//查询单条
public static function get($id){
$db = Yii::$app->db->createCommand('SELECT `id`,`name`,`num` FROM ' . self::$_tbl . ' WHERE id=:id');
$result = $db->bindValue(':id', $id)->queryOne();
if( is_array($result) ){
return $result;
}
return array();
}
//添加
public static function add(array &$params){
$res=Yii::$app->db->createCommand()->insert(self::$_tbl,[
'name'=>$params['name'],
'num'=>$params['num'],
])->execute();
if($res){
return true;
}else{
return false;
}
} //修改
public static function modify($id,array &$params){
$res=Yii::$app->db->createCommand()->update(self::$_tbl,$params,'id='.$id)->execute();
if($res===0){
return false;
}
return true;
}
//删除
public static function del($id){
$res=Yii::$app->db->createCommand()->delete(self::$_tbl,'id='.$id)->execute();
if($res===0){
return false;
}
return true;
} } (4)模块初始化 <?php
namespace frontend\modules\v1;
class Module extends \yii\base\Module{
public $controllerNamespace = 'frontend\modules\v1\controllers';
public function init(){
parent::init();
}
}