yii2进行接口开发,比较优化的curd接口(提供sql)全方位提供
【摘要】
第一:虚拟域名,看我博客,就知道啦.美化url也是啦(必须进行url美化+虚拟域名才能使用啊啊啊) 第一步: 把backend复制一份,改名为api. 第二步: 在common/config/bootst...
第一:虚拟域名,看我博客,就知道啦.美化url也是啦(必须进行url美化+虚拟域名才能使用啊啊啊)
第一步:
把backend复制一份,改名为api.
第二步:
在common/config/bootstrap.php下粘贴下面这个代码.
Yii::setAlias('@api', dirname(dirname(__DIR__)) . '/api');
- 1
第三步:在api/config/main-local.php下粘贴以下这句话。
<?php
$config = [
'components' => [
'request' => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => 'qixaipxaipscn',
'parsers' => [
'application/json' => 'yii\web\JsonParser'
]
],
],
];
if (!YII_ENV_TEST) {
// configuration adjustments for 'dev' environment
$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = [
'class' => 'yii\debug\Module',
];
$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = [
'class' => 'yii\gii\Module',
];
}
return $config;
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
第四步:
在api/config/main.php下,粘贴这句代码.
<?php
$params = array_merge(
require(__DIR__ . '/../../common/config/params.php'),
require(__DIR__ . '/../../common/config/params-local.php'),
require(__DIR__ . '/params.php'),
require(__DIR__ . '/params-local.php')
);
return [
'id' => 'app-api',
'basePath' => dirname(__DIR__),
'controllerNamespace' => 'api\controllers',
'bootstrap' => ['log'],
'modules' => [],
'components' => [
'request' => [
'csrfParam' => '_csrf-api',
],
'user' => [
'identityClass' => 'common\models\User',
'enableAutoLogin' => true,
'identityCookie' => ['name' => '_identity-api', 'httpOnly' => true],
],
'session' => [
// this is the name of the session cookie used for login on the api
'name' => 'advanced-api',
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'errorHandler' => [
'errorAction' => 'site/error',
],
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
['class' => 'yii\rest\UrlRule', 'controller' => 'article'],
],
],
],
'params' => $params,
];
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
然后在api/controller/下新建一个叫做ArticleController.php的文件哦(复制粘贴即可)
<?php
namespace api\controllers;
use common\models\Article;
use Yii;
use yii\web\Controller;
class ArticleController extends \yii\rest\Controller
{
//增加
public function actionCreate()
{
return "创建的接口调通啦";
}//查询
public function actionIndex()
{
return "列表(查询)的接口调通啦";
}//删除
public function actionDelete($id)
{
return "删除的接口调通啦";
}//更新
public function actionUpdate($id)
{
return "更新的接口调通啦";
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
创建接口调通啦
查询接口调通啦
删除接口调通啦
更新接口调通啦
切记,这里的(更新删除)1代表id.必须有数据才能进行切记切记.
-- phpMyAdmin SQL Dump
-- version 4.5.1
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: 2022-05-03 13:26:46
-- 服务器版本: 10.1.13-MariaDB
-- PHP Version: 5.6.21
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Database: `a`
--
-- --------------------------------------------------------
--
-- 表的结构 `curd`
--
CREATE TABLE `curd` (
`id` int(20) NOT NULL,
`phone` varchar(20) COLLATE utf8_vietnamese_ci NOT NULL,
`name` varchar(255) COLLATE utf8_vietnamese_ci NOT NULL,
`address` text COLLATE utf8_vietnamese_ci NOT NULL,
`time` varchar(255) COLLATE utf8_vietnamese_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_vietnamese_ci;
--
-- 转存表中的数据 `curd`
--
INSERT INTO `curd` (`id`, `phone`, `name`, `address`, `time`) VALUES
(16, '13647592401', 'cyg666', '海南省琼海市', '2022-05-03 13:24:37');
--
-- Indexes for dumped tables
--
--
-- Indexes for table `curd`
--
ALTER TABLE `curd`
ADD PRIMARY KEY (`id`);
--
-- 在导出的表使用AUTO_INCREMENT
--
--
-- 使用表AUTO_INCREMENT `curd`
--
ALTER TABLE `curd`
MODIFY `id` int(20) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=17;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
兄弟们,然后gii:model curd表哈”
<?php
namespace api\controllers;
use common\models\Curd;
use Yii;
class ArticleController extends \yii\rest\Controller
{
//增加
public function actionCreate()
{
$request = Yii::$app->request;
Yii::$app->db->createCommand()->insert('curd', [
'name' => $request->post("name"),
'phone' => $request->post("phone"),
'address'=> $request->post("address"),
'time'=>date("Y-m-d H:i:s"),
])->execute();
}//查询全部
public function actionIndex()
{
$data = \Yii::$app->db->createCommand('SELECT * FROM curd')
->queryAll();
$json = json_encode(array(
"resultCode"=>200,
"message"=>"新增成功!",
"data"=>$data
),JSON_UNESCAPED_UNICODE);
//转换成字符串JSON
echo($json);
}//删除
public function actionDelete($id)
{
return Curd::findOne([ 'id' => $id ])->delete()?"删除{$id}成功":"删除{$id}失败";
}//更新
public function actionUpdate($id)
{
$request = Yii::$app->request;
$data= Yii::$app->db->createCommand()->update("curd",[
'name' => $request->post("name"),
'phone' => $request->post("phone"),
'address'=> $request->post("address"),
'time'=>date("Y-m-d H:i:s"),
],['id'=>$id])->execute();
$json = json_encode(array(
"resultCode"=>200,
"message"=>"更新成功!",
"data"=>$data,
),JSON_UNESCAPED_UNICODE);
//转换成字符串JSON
echo($json);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
create:
查询:
修改:
删除:
文章来源: blog.csdn.net,作者:贵哥的编程之路(热爱分享),版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/qq_37805832/article/details/124560045
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)