as3291363 发表于 2020-11-23 15:08:09

楼主 你用CI4 是有用 composer 来进行安装吗?
还是直接下载CI4本地部署

连普科技 发表于 2020-11-25 16:10:10

as3291363 发表于 2020-11-23 15:08
楼主 你用CI4 是有用 composer 来进行安装吗?
还是直接下载CI4本地部署

手动安装的,不喜欢用 composer

yoyoyuye 发表于 2021-9-10 15:47:12

本帖最后由 yoyoyuye 于 2021-9-10 15:53 编辑

基于CI4 自带的restFull。
/*
Navicat MySQL Data Transfer

Source Server         : localhost
Source Server Version : 50726
Source Host         : localhost:3306
Source Database       : ci4

Target Server Type    : MYSQL
Target Server Version : 50726
File Encoding         : 65001

Date: 2021-09-10 15:43:02
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `my_news`
-- ----------------------------
DROP TABLE IF EXISTS `my_news`;
CREATE TABLE `my_news` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`auth` varchar(20) NOT NULL,
`img_path` varchar(100) NOT NULL,
`title` varchar(128) NOT NULL,
`slug` varchar(128) NOT NULL,
`body` text NOT NULL,
PRIMARY KEY (`id`),
KEY `slug` (`slug`)
) ENGINE=MyISAM AUTO_INCREMENT=13 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of my_news
-- ----------------------------
INSERT INTO `my_news` VALUES ('1', 'admin', './public/uploads/images/logo.png', '测试数据一条11112222', '什么意思呢', '何必你好1212');
INSERT INTO `my_news` VALUES ('11', '', '', '再加一个', '加一个什么呢', '看不看得到啊');
INSERT INTO `my_news` VALUES ('12', '', '', '随便写', '写一个什么呢啊啊a', 'a阿萨v阿萨阿斯顿啊啊啊啊阿萨');
INSERT INTO `my_news` VALUES ('3', '', '', 'Caffeination, Yes!', 'caffeination-yes', 'World\'s largest coffee shop␣\r\n都是啥玩意');
INSERT INTO `my_news` VALUES ('5', '', '', '呵呵呵呵', '士大夫打撒', '士大夫大师傅撒飞洒发生发生发生飞洒发生');
INSERT INTO `my_news` VALUES ('6', '', '', '温德尔瓦', '巍峨哇', '温热嗡嗡嗡我认为');
INSERT INTO `my_news` VALUES ('7', '', '', '取桥桥', '的撒范德萨', '是否是否打算打发发书法大赛v士大夫打撒');
INSERT INTO `my_news` VALUES ('8', '', '', '胜多负少', '士大夫打撒是', '士大夫打撒');
INSERT INTO `my_news` VALUES ('9', '', '', '331我顶顶顶顶是', '首发式发生', '上的发大水发大水发大水发生发生发啊沙发沙发阿斯顿发顺丰');


model:


<?php
namespace App\Models\Apimodel;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
class News_model extends \CodeIgniter\Model{
    protected$table='my_news';
    protected $primarykey='id';
    protected$allowedFields=[
      'auth',
      'title',
      'img_path',
      'slug',
      'body',
    ];
}



控制器:

<?php
namespace App\Controllers\Api;
use CodeIgniter\RESTful\ResourceController;
use CodeIgniter\API\ResponseTrait;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
class News extends ResourceController{
    use ResponseTrait;
    public function index(){
      $model=New \App\Models\Apimodel\News_model();
      $data=$model->findAll();
      return $this->respond($data);
    }
    public function show($id=null){
      $model=new \App\Models\Apimodel\News_model();
      $data=$model->getwhere(['id'=>$id])->getResult();
      if($data){
            return $this->respond($data);
      }else{
            return $this->failNotFound("没有找到这个id=".$id."的数据.");
      }
    }
    public function add(){
      $model=new \App\Models\Apimodel\News_model();
      $data['auth']=$this->request->getVar('auth');
      $data['img_path']=$this->request->getVar('img_path');
      $data['title']=$this->request->getVar('title');
      $data['slug']=$this->request->getVar('slug');
      $data['body']=$this->request->getVar('body');
      $model->insert($data);
      $response=[
            'status'=>201,
            'error'=>null,
            'message'=>[
                'success'=>'数据添加成功',
            ]
      ];
      return $this->respondCreated($response);
    }
    public function update($id=null){
      $model=New \App\Models\Apimodel\News_model();
      $input=$this->request->getRawInput();
      $data=[
            'auth'=>$input['auth'],
            'img_path'=>$input['img_path'],
            'title'=>$input['title'],
            'slug'=>$input['slug'],
            'body'=>$input['body'],
      ];
      $model->update($id,$data);
      $response=[
            'status'=>200,
            'error'=>null,
            'message'=>[
                'success'=>'数据更新成功',
            ],
      ];
      return $this->respond($response);
    }
    public function del($id=null){
      $model=new \App\Models\Apimodel\News_model();
      $data=$model->find($id);
      if($data){
            $model->delete($id);
            $response=[
                'status'=>200,
                'error'=>null,
                'message'=>[
                  'success'=>'数据删除成功',
                ]
            ];
            return $this->respondDeleted($response);
      }else{
            return $this->failNotFound("没有找到这个id=".$id."的数据.");
      }
    }
}



连普科技 发表于 2021-9-11 10:31:20

yoyoyuye 发表于 2021-9-10 15:47
基于CI4 自带的restFull。
model:



restfull api 例子吗 我拿走了:D
页: 1 [2]
查看完整版本: 欢迎交流CI4实际开发中的问题,本人正在开发的小项目