yii2 restful api 诡异的问题, 请求成功后数据竟然没有写入数据表 [ 2.0 版本 ]
本项目是按照权威指南中, https://www.yiichina.com/doc/guide/2.0/tutorial-yii-as-micro-framework 实现的。
PostController.php 代码
`
<?php
namespace micro\controllers;
use yii\rest\ActiveController;
class PostController extends ActiveController
{
public $modelClass = 'micro\models\Post';
public function behaviors()
{
// remove rateLimiter which requires an authenticated user to work
$behaviors = parent::behaviors();
unset($behaviors['rateLimiter']);
return $behaviors;
}
}
Post.php 代码:
<?php
namespace micro\models;
use yii\db\ActiveRecord;
class Post extends ActiveRecord
{
public static function tableName()
{
return '{{post}}';
}
}
`
config.php 配置文件:
`
<?php
return [
'id' => 'micro-app',
// the basePath of the application will be the `micro-app` directory
'basePath' => __DIR__,
// this is where the application will find all controllers
'controllerNamespace' => 'micro\controllers',
// set an alias to enable autoloading of classes from the 'micro' namespace
'aliases' => [
'@micro' => __DIR__,
],
'components' => [
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=yii2micro',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8mb4',
],
],
];
基于phpstorm client 的测试代码:
GET http://localhost:8080/index.php?r=post
Accept: application/json
GET http://localhost:8080/index.php?r=post/view&id=1
Accept: application/json
POST http://localhost:8080/index.php?r=post/create
Content-Type: application/x-www-form-urlencoded
title=BBB&body=BBB&_method=POST
PUT http://localhost:8080/index.php?r=post/update&id=2
Content-Type: application/json
{
"title": "AAA",
"body": "AAAA"
}
DELETE http://localhost:8080/index.php?r=post/delete&id=1
Content-Type: application/json
`
共 0 个回答
frans
最后登录:2022-11-10
在线时长:21小时34分
- 粉丝7
- 金钱1535
- 威望20
- 积分1945