thinkphp5.1 アノテーション プラグイン
只是没有如果
只是没有如果 2020-01-11 16:07:51
0
0
1178

thinkphp5.1 のアノテーションによって実装:

データバリデータ

選択項目の取得、パラメータの書式設定

Attribute オブジェクトを自動的に挿入

自動トランザクション

使用:
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;
   }
}

詳細なドキュメント

只是没有如果
只是没有如果

全員に返信(0)
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!