创建模块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 接口调用,增删改查的更多相关文章

  1. 百度鹰眼Java接口调用增删改查实例

    因感觉百度鹰眼的使用场景比较符合实际业务,于是对百度鹰眼做了简单功能调试.刚开始使用springframework封装的RestTemplate,但是测试提示ak参数不存在.后又试了几种方法,均提示a ...

  2. MySQL数据库学习笔记(九)----JDBC的ResultSet接口(查询操作)、PreparedStatement接口重构增删改查(含SQL注入的解释)

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

  3. springmvc+spring3+hibernate4框架简单整合,简单实现增删改查功能

    转自:https://blog.csdn.net/thinkingcao/article/details/52472252 C 所用到的jar包     数据库表 数据库表就不用教大家了,一张表,很简 ...

  4. SSH框架下的多表增删改查

    下载地址:SSH框架下的多表增删改查 点击进入码云Git下载 点击进入CSDN下载 项目结构: 项目代码就不全部贴出来了,只贴下核心代码.需要项目的自己可以去下载. package com.atgui ...

  5. 基于SSM之Mybatis接口实现增删改查(CRUD)功能

    国庆已过,要安心的学习了. SSM框架以前做过基本的了解,相比于ssh它更为优秀. 现基于JAVA应用程序用Mybatis接口简单的实现CRUD功能: 基本结构: (PS:其实这个就是用的Mapper ...

  6. nodejs+express+mysql实现restful风格的增删改查示例

    首先,放上项目github地址:https://github.com/codethereforam/express-mysql-demo 一.前言 之前学的java,一直用的ssm框架写后台.前段时间 ...

  7. 进入全屏 nodejs+express+mysql实现restful风格的增删改查示例

    首先,放上项目github地址:https://github.com/codethereforam/express-mysql-demo 一.前言 之前学的java,一直用的ssm框架写后台.前段时间 ...

  8. 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 整合 ...

  9. ssm框架(Spring Springmvc Mybatis框架)整合及案例增删改查

    三大框架介绍 ssm框架是由Spring springmvc和Mybatis共同组成的框架.Spring和Springmvc都是spring公司开发的,因此他们之间不需要整合.也可以说是无缝整合.my ...

随机推荐

  1. 万能的DBHelper帮助类

    DBHelper类: 简单的理解就是一个工具箱,我要用锤子的时候就在里面拿,我要用剪刀的时候也可以在里面拿,前提是我们写的DBHelper够不够强大! 软件中的四大功能:增.删.改.查  我们要实现这 ...

  2. ORACLE删除分区

    业务需求:定期删除表中三个月之前的数据 说明:由于表采取一个月一个分区的设计,所以删除三个月之前的数据也就是删除三个月之前的分区.但需要注意的是删除分区后全局索引会失效,而本地local索引不会受到影 ...

  3. [LeetCode] 33. Search in Rotated Sorted Array_Medium tag: Binary Search

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

  4. Python基础(八) yaml在python中的使用

    yaml 通常用来存储数据,类似于json YAML 简介 YAML(Yet Another Markup Language),一种直观的能够被电脑识别的数据序列化格式,是一个可读性高并且容易被人类阅 ...

  5. git----------SourceTree如何连接ssh的仓库地址,这里记录的是客户端需要做的事

    1. 2. 打开命令行以后输入ssh-keygen -t rsa -C "example@example.com"  这个地方输入的是你注册gitlab所使用的邮箱.按回车,之后还 ...

  6. Software Testing 3

    Questions: • 7. Use the following method printPrimes() for questions a–d. 基于Junit及Eclemma(jacoco)实现一 ...

  7. 深入浅出JAVA线程池使用原理2

    一.Executor框架介绍 Executor框架将Java多线程程序分解成若干个任务,将这些任务分配给若干个线程来处理,并得到任务的结果 1.1.Executor框架组成 任务:被执行任务需要实现的 ...

  8. (转) SpringMVC学习笔记-

    一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要的jar包. 2.添加Web.xml配置文件中关于SpringMVC的配置 <!--conf ...

  9. myBatis学习之路1-基本功能实现

    myBatis也是一个持久型框架,相较于hibernate来说,算是轻量级的. 1.配置mybatis环境 相关jar下载地址:mybatis+mysalJAR包 2.新建一个java project ...

  10. ltp-ddt inverted_return小trick

    ./runtest/ddt/i2c_readwrite # @name I2C write read test on slave device# @desc I2C write read test o ...