url:美化看这篇文章
https://blog.csdn.net/qq_37805832/article/details/122087080
sql:
-- 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 */;
<?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;
注意一下这里:
'request' => [
'parsers' => [
'application/json' => 'yii\web\JsonParser',
]
]
和这里:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
[
'class' => 'yii\rest\UrlRule',
'controller' => 'v1/news'
]
],
]
然后是:
'modules' => [
'v1' => [
'class' => 'app\modules\v1\Module',
],
],
放在这里:
然后是:
生成数据库中news表的gii:model放进app\models里面
然后是:生成控制器:
然后是修改控制器:app\modules\v1\controllers\NewsController.php
<?php
namespace app\modules\v1\controllers;
class NewsController extends \yii\rest\ActiveController
{
public $modelClass = 'app\models\News';
}
创建:
查看:
删除
更新: