2021-07-12 18:22:09 1118次浏览 0条回答 0 悬赏 70 金钱

本项目是按照权威指南中, 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

`

    没有找到数据。
您需要登录后才可以回答。登录 | 立即注册
frans
经理

frans

注册时间:2012-07-05
最后登录:2022-11-10
在线时长:21小时34分
  • 粉丝7
  • 金钱1535
  • 威望20
  • 积分1945

热门问题