Yii框架实现restful 接口调用,增删改查
创建模块modules;
在main.php中配置文件:(1)


(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();
}
}
Yii框架实现restful 接口调用,增删改查的更多相关文章
- 百度鹰眼Java接口调用增删改查实例
因感觉百度鹰眼的使用场景比较符合实际业务,于是对百度鹰眼做了简单功能调试.刚开始使用springframework封装的RestTemplate,但是测试提示ak参数不存在.后又试了几种方法,均提示a ...
- MySQL数据库学习笔记(九)----JDBC的ResultSet接口(查询操作)、PreparedStatement接口重构增删改查(含SQL注入的解释)
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...
- springmvc+spring3+hibernate4框架简单整合,简单实现增删改查功能
转自:https://blog.csdn.net/thinkingcao/article/details/52472252 C 所用到的jar包 数据库表 数据库表就不用教大家了,一张表,很简 ...
- SSH框架下的多表增删改查
下载地址:SSH框架下的多表增删改查 点击进入码云Git下载 点击进入CSDN下载 项目结构: 项目代码就不全部贴出来了,只贴下核心代码.需要项目的自己可以去下载. package com.atgui ...
- 基于SSM之Mybatis接口实现增删改查(CRUD)功能
国庆已过,要安心的学习了. SSM框架以前做过基本的了解,相比于ssh它更为优秀. 现基于JAVA应用程序用Mybatis接口简单的实现CRUD功能: 基本结构: (PS:其实这个就是用的Mapper ...
- nodejs+express+mysql实现restful风格的增删改查示例
首先,放上项目github地址:https://github.com/codethereforam/express-mysql-demo 一.前言 之前学的java,一直用的ssm框架写后台.前段时间 ...
- 进入全屏 nodejs+express+mysql实现restful风格的增删改查示例
首先,放上项目github地址:https://github.com/codethereforam/express-mysql-demo 一.前言 之前学的java,一直用的ssm框架写后台.前段时间 ...
- SSH(Struts 2.3.31 + Spring 4.1.6 + Hibernate 5.0.12 + Ajax)框架整合实现简单的增删改查(包含分页,Ajax 无刷新验证该用户是否存在)
软件152 余建强 该文将以员工.部门两表带领大家进入SSH的整合教程: 源码下载:http://download.csdn.net/detail/qq_35318576/9877235 SSH 整合 ...
- ssm框架(Spring Springmvc Mybatis框架)整合及案例增删改查
三大框架介绍 ssm框架是由Spring springmvc和Mybatis共同组成的框架.Spring和Springmvc都是spring公司开发的,因此他们之间不需要整合.也可以说是无缝整合.my ...
随机推荐
- 安装Linux操作系统,学习Liunx基础
安装Linux操作系统 遇到的问题以及解决方法 问题1:安装虚拟机时出现以下界面 解决方法 我的电脑--右击--管理--服务和应用服务--服务--在服务里启动:Device Install Servi ...
- Django基本配置与URLconf
what's the Django python的框架主要有:Django.Flask.Tornado Django是一个开放源代码的Web应用框架,由Python写成.它的主要特点是大而全,我们需要 ...
- PHP中array_merge和array+array的区别
在PHP中可以使用array_merge函数和两个数组相加array+array的方式进行数组合并,但两者效果并不相同,区别如下: 当下标为数值时,array_merge()不会覆盖掉原来的值,但ar ...
- tp5 model controlle sql
model::::use think\Db 引用db库类 用于数据库之类use think\Model 引用模板use think\Cookie 引用传值 $rs=Db::name(‘表名’)-> ...
- LinkedHashMap和HashTable
LinkedHashMap: 继承了HashMap: 其中,key不允许重复是Map接口就有的性质: HashTable: 同步的,意味着是单线程,意味着线程安全的,但是速度慢,和List接口集合的子 ...
- vue使用桌面Element-UI和移动端MintUI的UI框架
vue使用桌面Element-UI和移动端MintUI的UI框架 element-uiElement - 网站快速成型工具http://element-cn.eleme.io/#/zh-CN 安装:n ...
- MySQL8.0 优化
参考 https://dev.mysql.com/doc/refman/8.0/en/insert-optimization.html https://dev.mysql.com/doc/refman ...
- elasticsearch best_fields most_fields cross_fields从内在实现看区别——本质就是前两者是以field为中心,后者是词条为中心
1.最佳字段(Best fields):: 假设我们有一个让用户搜索博客文章的网站(允许多字段搜索,最佳字段查询),就像这两份文档一样: PUT /my_index/my_type/1 { " ...
- ★Pandas 零碎知识
1 修改属性 1.1 修改1列的类型属性: df['总金额'] = pd.to_numeric(df['总金额']) #转变dataframe的1列为数值型 1.2 多列设为数值型:(使用DataFr ...
- HDU 4010 Query on The Trees(动态树)
题意 给定一棵 \(n\) 个节点的树,每个点有点权.完成 \(m\) 个操作,操作四两种,连接 \((x,y)\) :提 \(x\) 为根,并断 \(y\) 与它的父节点:增加路径 \((x,y)\ ...