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

2022-03-25 20:49:36 浏览数 (1)

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' => 'yiiwebJsonParser',
    ]
        ],
        'cache' => [
            'class' => 'yiicachingFileCache',
        ],
        'user' => [
            'identityClass' => 'appmodelsUser',
            'enableAutoLogin' => true,
        ],
        'errorHandler' => [
            'errorAction' => 'site/error',
        ],
        'mailer' => [
            'class' => 'yiiswiftmailerMailer',
            // 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' => 'yiilogFileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'db' => $db,
        
        'urlManager' => [
        'enablePrettyUrl' => true,
        'showScriptName' => false,
        'rules' => [
        [
            'class' => 'yiirestUrlRule',
            'controller' => 'v1/news'
        ]
        
     ],

        ]

        
    ],
    'params' => $params,
    'modules' => [
        'v1' => [
            'class' => 'appmodulesv1Module',
        ],
    ],
];

if (YII_ENV_DEV) {
    // configuration adjustments for 'dev' environment
    $config['bootstrap'][] = 'debug';
    $config['modules']['debug'] = [
        'class' => 'yiidebugModule',
        // 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' => 'yiigiiModule',
        // 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' => 'yiiwebJsonParser',
    ]
]

和这里:

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

然后是:

代码语言:javascript复制
'modules' => [
        'v1' => [
            'class' => 'appmodulesv1Module',
        ],
    ],

放在这里:

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

然后是修改控制器:appmodulesv1controllersNewsController.php

代码语言:javascript复制
<?php

namespace appmodulesv1controllers;

class NewsController extends yiirestActiveController
{
	 public $modelClass = 'appmodelsNews';
}

创建:

查看:

删除

更新:

0 人点赞