前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >yii接口开发curd,提供全部(基础版本basic)RestfulApi

yii接口开发curd,提供全部(基础版本basic)RestfulApi

作者头像
贵哥的编程之路
发布2022-03-25 20:49:36
4730
发布2022-03-25 20:49:36
举报

url:美化看这篇文章

代码语言:javascript
复制
https://blog.csdn.net/qq_37805832/article/details/122087080

sql:

代码语言:javascript
复制
-- phpMyAdmin SQL Dump
-- version 4.5.1
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: 2022-03-02 04:08:09
-- 服务器版本: 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: `qq`
--

-- --------------------------------------------------------

--
-- 表的结构 `news`
--

CREATE TABLE `news` (
  `id` int(10) UNSIGNED NOT NULL,
  `title` varchar(50) NOT NULL,
  `thumb` varchar(250) DEFAULT NULL,
  `content` text,
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- 转存表中的数据 `news`
--

INSERT INTO `news` (`id`, `title`, `thumb`, `content`, `created_at`, `updated_at`) VALUES
(1, '1111', NULL, '1111', '0000-00-00 00:00:00', '0000-00-00 00:00:00'),
(2, '2222', NULL, '1111', '0000-00-00 00:00:00', '0000-00-00 00:00:00'),
(3, '1111', 'upload/火狐截图_2017-08-21T03-07-27.499Z.png', '1111', '0000-00-00 00:00:00', '0000-00-00 00:00:00'),
(4, 'Hello World', NULL, NULL, '2017-08-21 10:25:17', '0000-00-00 00:00:00'),
(5, 'ppp', NULL, NULL, '2022-03-02 03:07:33', '0000-00-00 00:00:00');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `news`
--
ALTER TABLE `news`
  ADD PRIMARY KEY (`id`);

--
-- 在导出的表使用AUTO_INCREMENT
--

--
-- 使用表AUTO_INCREMENT `news`
--
ALTER TABLE `news`
  MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=6;
/*!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 */;
在这里插入图片描述
在这里插入图片描述
代码语言:javascript
复制
<?php

$params = require __DIR__ . '/params.php';
$db = require __DIR__ . '/db.php';

$config = [
    'id' => 'basic',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
    'aliases' => [
        '@bower' => '@vendor/bower-asset',
        '@npm'   => '@vendor/npm-asset',
    ],
    'components' => [
        'request' => [
            // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
            'cookieValidationKey' => '69PsZ-icrXqKv5ww8q_yDKP4vN1VdBhi',
            'parsers' => [
        'application/json' => 'yii\web\JsonParser',
    ]
        ],
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],
        'user' => [
            'identityClass' => 'app\models\User',
            'enableAutoLogin' => true,
        ],
        'errorHandler' => [
            'errorAction' => 'site/error',
        ],
        'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            // send all mails to a file by default. You have to set
            // 'useFileTransport' to false and configure transport
            // for the mailer to send real emails.
            'useFileTransport' => true,
        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'db' => $db,
        
        'urlManager' => [
        'enablePrettyUrl' => true,
        'showScriptName' => false,
        'rules' => [
        [
            'class' => 'yii\rest\UrlRule',
            'controller' => 'v1/news'
        ]
        
     ],

        ]

        
    ],
    'params' => $params,
    'modules' => [
        'v1' => [
            'class' => 'app\modules\v1\Module',
        ],
    ],
];

if (YII_ENV_DEV) {
    // configuration adjustments for 'dev' environment
    $config['bootstrap'][] = 'debug';
    $config['modules']['debug'] = [
        'class' => 'yii\debug\Module',
        // uncomment the following to add your IP if you are not connecting from localhost.
        //'allowedIPs' => ['127.0.0.1', '::1'],
    ];

    $config['bootstrap'][] = 'gii';
    $config['modules']['gii'] = [
        'class' => 'yii\gii\Module',
        // uncomment the following to add your IP if you are not connecting from localhost.
        //'allowedIPs' => ['127.0.0.1', '::1'],
    ];
}

return $config;

注意一下这里:

在这里插入图片描述
在这里插入图片描述
代码语言:javascript
复制
'request' => [
    'parsers' => [
        'application/json' => 'yii\web\JsonParser',
    ]
]

和这里:

代码语言:javascript
复制
 'urlManager' => [
        'enablePrettyUrl' => true,
        'showScriptName' => false,
        'rules' => [
        [
            'class' => 'yii\rest\UrlRule',
            'controller' => 'v1/news'
        ]
        
     ],
        ]

然后是:

在这里插入图片描述
在这里插入图片描述
代码语言:javascript
复制
'modules' => [
        'v1' => [
            'class' => 'app\modules\v1\Module',
        ],
    ],

放在这里:

在这里插入图片描述
在这里插入图片描述

然后是: 生成数据库中news表的gii:model放进app\models里面 然后是:生成控制器:

在这里插入图片描述
在这里插入图片描述

然后是修改控制器:app\modules\v1\controllers\NewsController.php

代码语言:javascript
复制
<?php

namespace app\modules\v1\controllers;

class NewsController extends \yii\rest\ActiveController
{
	 public $modelClass = 'app\models\News';
}

创建:

在这里插入图片描述
在这里插入图片描述

查看:

在这里插入图片描述
在这里插入图片描述

删除

在这里插入图片描述
在这里插入图片描述

更新:

在这里插入图片描述
在这里插入图片描述
本文参与?腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-03-02 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客?前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体分享计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 MariaDB
腾讯云数据库 MariaDB(TencentDB for MariaDB) 让您轻松在云端部署、使用 MariaDB 数据库。MariaDB 是在 MySQL 版权被 Oracle 收购后,由 MySQL 创始人 Monty 创立,其版权授予了“MariaDB基金会(非营利性组织)”以保证 MariaDB 永远开源,良好的开源策略,是企业级应用的最优选择,主流开源社区系统/软件的数据库系统,均已默认配置 MariaDB。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
http://www.vxiaotou.com