thinkphp5.1註解插件

前言:

thinkphp5.1中用註解的方式實現:php

v0.1.0版本html

  • 數據驗證器
  • 請求過濾、格式化
  • 屬性對象注入

dev-master版本 額外支持git

  • 自動事務
  • 數據緩存

 

若是您以爲好用,點個star哈
github地址:https://github.com/cshaptx4869/thinkphp-annotationgithub

 

安裝

穩定版:composer require cshaptx4869/thinkphp-annotation
最新版:composer require cshaptx4869/thinkphp-annotation:dev-master

配置

tags.php 添加行爲用於控制器註解掃描thinkphp

'action_begin' => [ \Fairy\ControllerAnnotationScaner::class ]

模型中使用註解的話要 在模型中引入 ModelAnnotationScaner  的traitshell

use \Fairy\ModelAnnotationScaner;

添加 system.php 配置文件(可選)json

return [ 'annotation' => [ 'cache' => false,// 是否開啓註解讀取緩存,默認false
        'writelist' => []// 註解讀取白名單,默認[]
        'interceptor' => [// 註解攔截器相關配置
            'enable' => true,// 默認開啓註解攔截器
            'whitelist' => []// 註解攔截器白名單,默認[]
        ],
        'validate' => [ 'callback' => function($msg) { // 自定義驗證錯誤信息後續處理
 } ] ] ]

PS:默認驗證器註解驗證不經過會終止程序運行並返回json格式的驗證錯誤信息。可經過配置 callback函數自定義後續處理。請注意,不一樣版本使用上會有些許差異。緩存

支持的註解

v0.1.0
註解名 申明範圍 做用
@Autowire 屬性 自動注入類對象
@DynamicAutowire 方法 聲明當前方法容許屬性注入的類
@IgnoreAutowire 方法 聲明當前方法忽略屬性注入的類
@RequestParam 方法 過濾、格式化請求參數
@Validator 方法 驗證器驗證

                              

dev-master
註解名 申明範圍 做用
@Autowire 屬性 自動注入類對象
@Transaction 方法 開啓事務返回值等價於true就會自動commit,不然rollback
@RequestParam 方法 過濾、格式化請求參數
@Validator 方法 驗證器驗證
@DataCache 方法 方法數據緩存

版本差別:

dev-master新增:app

Transaction 註解composer

Transaction 註解根據當前方法返回值自動判斷事務後續處理,如返回值等價於true就會自動commit,不然rollback。

Transaction 註解須要搭配 Autowire註解使用,且不支持在控制器中使用,推薦在模型中使用。

DataCache 註解

被DataCache註解修飾的方法的返回數據會被緩存。

ModelAnnotationScaner

支持模型中使用屬性註解

 

Autowire 註解改動:

v0.1.0 版本中 Autowire 註解必須寫class屬性,如 Autowire(class=ArticleModel::class),而在v0.1.1版本中 Autowire 註解則沒有class屬性而是經過@var ArticleModel 註解來自動識別。

使用 dev-master 版本

ArticleController 控制器:

<?php namespace app\index\controller; use app\index\validate\Article\SaveValidate; use app\common\model\ArticleModel; // 引入對應的註解
use Fairy\Annotation\Autowire; use Fairy\Annotation\RequestParam; use Fairy\Annotation\Validator; use think\Request; class ArticleController { /** * 屬性對象注入 * @Autowire()
* @var ArticleModel
*/ public $articleModel; /** * 數據驗證 * clsss: thinkphp定義的驗證器類名(必填) string類型 * scene: 驗證場景名 string類型 * batch:是否批量驗證 bool類型 * throw: 驗證失敗是否拋出異常 bool類型 * @Validator( * class=SaveValidate::class, * scene="save", * batch=false, * throw=false * ) * * 獲取參數 * fields: 定義要獲取的字段名,可批量設置默認值 array類型 * mapping: 轉換前臺傳遞的字段名爲自定義的字段名 array類型 * method: 獲取參數的方法,支持get、post、put、delte string類型 * json: 格式化json字段的數據 array類型 * * json使用示例: * json:{field1,field2,...fieldn} * 表示格式化field1,field2,...,字段的json數據 * * 支持json一維和二維字段的涮選,如 * json:{field1:{childField1,childField2...}} * 表示格式化field1字段的json數據,並只獲取field1字段下的childField1和childField2下標的值(支持深度一維和二維,會自動識別) * * @RequestParam( * fields={"title","image_url","content","category_id","is_temporary","extra":"默認值"}, * json={"category_id"}, * mapping={"image_url":"img_url"}, * method="post" * ) */ public function save(Request $request) { //獲取過濾事後的參數 $postData = $request->requestParam; return MyToolkit::success($this->articleModel->store($postData)); } }
AaaModel 模型
<?php namespace app\common\model; use Fairy\Annotation\Autowire; use Fairy\Annotation\Transaction; use Fairy\ModelAnnotationScaner; use think\Db; use think\Model; class AaaModel extends Model { // 模型中使用Autowire註解的trait
    use ModelAnnotationScaner; /** * @Autowire() * @var AccountModel */
    public $accountModel; /** * 註解控制事務 * 返回值等價於true commit 不然 rollback * @Transaction() */
    public function store() { return Db::name('aaa')->insert(['id' => 14, 'username' => 'bob']) > 0; }
/**
* 方法緩存
* @DataCache(expire="3m")
*/
public function index()
{
return Db::name('aaa')->page(1, 2)->order('id', 'desc')->select();
}
}

 

IDE 註解插件支持

一些ide已經提供了對註釋的支持,推薦安裝,以便提供註解語法提示

相關文章
相關標籤/搜索