第一:虚拟域名,看我博客,就知道啦.美化url也是啦(必须进行url美化+虚拟域名才能使用啊啊啊)
第一步:
把backend复制一份,改名为api.
第二步:
在common/config/bootstrap.php下粘贴下面这个代码.
Yii::setAlias('@api', dirname(dirname(__DIR__)) . '/api');
第三步:在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;
第四步:
在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,
];
然后在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代表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 */;
兄弟们,然后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);
}
}
create:
查询:
修改:
删除: