PHP code example of cshaptx4869 / thinkphp-annotation

1. Go to this page and download the library: Download cshaptx4869/thinkphp-annotation library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

cshaptx4869 / thinkphp-annotation example snippets


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

use \Fairy\ModelAnnotationScaner;

return [
    'annotation' => [
        'cache' => false,// 是否开启注解读取缓存,默认false
        'writelist' => []// 注解读取白名单,默认[]
        'interceptor' => [// 注解拦截器相关配置
            'enable' => true,// 默认开启注解拦截器
            'whitelist' => []// 注解拦截器白名单,默认[]
        ],
        'validate' => [
            'callback' => function($msg) {
            	// 自定义验证错误信息后续处理
            }
        ]
    ]
]



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(class=ArticleModel::class)
     */
    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));
    }



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));
    }
}



namespace app\common\model;

use Fairy\Annotation\Autowire;
use Fairy\Annotation\Transaction;
use Fairy\ModelAnnotationScaner;
use think\Db;
use think\Model;

class ArticleModel extends Model
{
    // 引入支持模型的属性使用注解的trait
    use ModelAnnotationScaner;

    /**
     * 注入对象
     * @Autowire()
     * @var ArticleCategoryRelationModel
     */
    public $articleCategoryRelationModel;
    
    /**
     * 注解控制事务
     * 返回值等价于true 事务自动 commit 否则 rollback
     * @Transaction()
     */
    public function store(array $params)
    {
        $categoryIds = $params['category_id'];
        unset($params['category_id']);
        $articleId = Db::name('article')->insertGetId($params);
        $realtion = array_map(function ($categoryId) use ($articleId) {
            return [
                'article_id' => $articleId,
                'category_id' => $categoryId
            ];
        }, $categoryIds);

        return $this->articleCategoryRelationModel->store($realtion);
    }
}