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 ...
随机推荐
- vue--vuex详解
安装vuex npm install vuex --save Vuex 什么是Vuex? 官方说法:Vuex 是一个专为 Vue.js应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的 ...
- django 1.11 目录
django 信号 django form
- linux 日常运维 目录
Linux防火墙:iptables Linux 抓包工具:tcpdump linux 查看磁盘读写:iotop linux 查看磁盘读写:iostat inux 查看网卡流量:sar linux 查看 ...
- python基础(15)-socket网络编程&socketserver
socket 参数及方法说明 初始化参数 sk = socket.socket(参数1,参数2,参数3) 参数1:地址簇 socket.AF_INET IPv4(默认) socket.AF_INET6 ...
- 一:window环境,LaTex快速安装(简单易懂)
一. 下载 清华开源软件镜像:点我下载 在线安装很容易失败,所以咱们选择ISO的~ 二. 安装 解压texlive2018.iso文件,并使用管理员权限打开install-tl-windo ...
- linux----------安装Supervisor是用Python开发的一套通用的进程管理程序
1.linux环境必须安装 python 2.yum install python-setuptools 3.获取supervisor包 wget https://pypi.python.org/pa ...
- Kindle:自动追更之云上之旅
2017年5月27: 原来的程序是批处理+Python脚本+Calibre2的方式,通过设定定时任务的方式,每天自动发动到自己的邮箱中.缺点是要一直开着电脑,又不敢放到服务器上~~ 鉴于最近公司查不关 ...
- ajax返回数据为undefined
在使用ajax异步请求后台返回数据后,使用console.log(data.message)打印返回数据,显示为undefined.苦恼了很久,终于在网上找到了答案. 先给大家看下异步代码: /*清零 ...
- webform ajax 异步请求
第一种就是对应方法的请求 虽然对应方法 但还是会刷新页面 webform是基于事件的 每次请求都会出发pageload <script> $(function () { $("# ...
- Nginx配置SSL证书实现https访问「浏览器未认证」
http 和 https 介绍 http:应用最广泛的一种网络协议,是一个B/S的request和response的标准,用于从www服务器传输超文本到本地浏览器的传输协议. https:以安全为目标 ...