Skip to content

Commit

Permalink
添加 restful Response Handler && Migration
Browse files Browse the repository at this point in the history
  • Loading branch information
forecho committed May 30, 2018
1 parent 64b129b commit 7780d22
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 0 deletions.
33 changes: 33 additions & 0 deletions Migration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* author : forecho <[email protected]>
* createTime : 2017/7/28 15:31
* description:
*/
namespace yiier\helpers;

class Migration extends \yii\db\Migration
{
/**
* 创建表选项
* @var string
*/
public $tableOptions = null;

/**
* 是否事务性存储表, 则创建为事务性表. 默认使用
* @var bool
*/
public $useTransaction = true;


public function init()
{
parent::init();

if ($this->db->driverName === 'mysql') { //Mysql 表选项
$engine = $this->useTransaction ? 'InnoDB' : 'MyISAM';
$this->tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=' . $engine;
}
}
}
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,52 @@ change config file, main.php
],
```


**ResponseHandler**

RESTful Response Handler, change config file `main.php`:

```php
'components' => [
'response' => [
'class' => 'yii\web\Response',
'on beforeSend' => function ($event) {
yii::createObject([
'class' => 'yiier\helpers\ResponseHandler',
'event' => $event,
])->formatResponse();
},
],
]
```

**Migration**

```php
<?php

use yiier\helpers\Migration;

class m170810_084615_create_post extends Migration
{
/**
* @var string
*/
public $tableName = '{{%post}}';

public function up()
{
$this->createTable($this->tableName, [
'id' => $this->primaryKey(),
'name' => $this->string()->notNull(),
], $this->tableOptions);
}

public function down()
{
$this->dropTable($this->tableName);
}

}
```
……
44 changes: 44 additions & 0 deletions ResponseHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* author : forecho <[email protected]>
* createTime : 2017/7/28 15:31
* description:
*/

namespace yiier\helpers;

class ResponseHandler
{
public $event;

/**
* 返回数据统一处理
*/
public function formatResponse()
{
$response = $this->event->sender;
if ($response->data !== null) {
if (isset($response->data['code']) && isset($response->data['message'])) {
$response->data = [
'code' => $response->data['code'] ?: $response->statusCode,
'data' => isset($response->data['data']) ? $response->data['data'] : null,
'message' => $response->data['message'],
];
} elseif ($response->format != 'html' && !isset($response->data['message'])) {
$response->data = [
'code' => 0,
'data' => $response->data,
'message' => '成功',
];
} elseif (isset($response->data['message']) && $response->data['message'] != "" && !isset($response->data['code'])) {
$message = $response->data['message'];
unset($response->data['message']);
$response->data = [
'code' => 0,
'data' => isset($response->data[0]) ? $response->data[0] : $response->data,
'message' => $message,
];
}
}
}
}

0 comments on commit 7780d22

Please sign in to comment.