Code Monkey home page Code Monkey logo

http-annotation-swagger's Introduction

EasySwoole Http Annotation Swagger


Install

The preferred way to install this extension is through composer.

Either run

composer require easyswoole/http-annotation-swagger

or add

"easyswoole/http-annotation-swagger": "dev-master"

to the require section of your composer.json file.

Config

dev.php add

<?php
    return [
            'swagger' => [
                'info' => [
                    'title' => '测试用例',
                    'version' => '1.0.0',
                    'contact' => ['name' => 'aaa'],
                ],
                'servers' => [
                    [
                        'url' => 'http://127.0.0.1:9501',
                        'description' => '本地环境'
                    ]
                ],
                'templates' => [
                    'success' => [
                        'code|状态码' => "0",
                        'result|api请求结果' => "{template}",
                        'msg|api提示信息' => 'success'
                    ],
                    'page' => [
                        'code|状态码' => "0",
                        'result|api请求结果' => [
                            'totalPage|总页数' => 10,
                            'page|页数' => 1,
                            'list|列表' => ["{template}"],
                        ],
                        'msg|api提示信息' => 'success'
                    ],
                ],
            ]
        ];

Use

xxxController.php

    <?php
        
        ....
        use EasySwoole\EasySwoole\Config;
        use EasySwoole\Spl\SplArray;
        use EasySwoole\HttpAnnotation\Swagger\AnnotationParser;
        use EasySwoole\HttpAnnotation\Swagger\GenerateSwagger;
        ....

        public function swagger()
        { 
             $config = new SplArray(Config::getInstance()->getConf("swagger"));
             $annotationParser = new AnnotationParser(EASYSWOOLE_ROOT . '/App/HttpController');
             $swagger = new GenerateSwagger($config, $annotationParser);
            
              // generate swagger.json
             $data = $swagger->scan2Json();
             $this->response()->write(json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
             $this->response()->withHeader('Content-type', 'application/json;charset=utf-8');
             $this->response()->withStatus(200);
            
             // or

             // generate swagger.html
             $string = $swagger->scan2Html();
             $this->response()->withAddedHeader('Content-type', "text/html;charset=utf-8");
             $this->response()->write($string);
        }   

Annotation ApiSuccessTemplate Use

xxxController.php

    <?php
    
    ....
    use EasySwoole\HttpAnnotation\Swagger\Annotation\ApiSuccessTemplate;

    ....
    

    /**
     * @Api(name="update",path="/Api/Admin/AdminUser/update")
     * @ApiDescription("更新数据")
     * @Method(allow={GET,POST})
     * @InjectParamsContext(key="param")
     * @ApiSuccessParam(name="code",description="状态码")
     * @ApiSuccessParam(name="result",description="api请求结果")
     * @ApiSuccessParam(name="msg",description="api提示信息")
     * @ApiSuccess({"code":200,"result":[],"msg":"更新成功"})
     * @ApiSuccessTemplate(template="page", result={
     *      "a|这是一个测试": 1,
     *      "b|第二个测试": "1"
     *    })
     * @ApiFail({"code":400,"result":[],"msg":"更新失败"})
     * @Param(name="adminId",alias="id",description="id",required="")
     * @Param(name="adminName",alias="昵称",description="昵称",lengthMax="32",optional="")
     * @Param(name="adminAccount",alias="账号",description="账号",lengthMax="32",optional="")
     * @Param(name="adminPassword",alias="密码",description="密码",lengthMax="32",optional="")
     */
    public function update()
    {
        
    }

http-annotation-swagger's People

Contributors

ice-leng avatar player626 avatar kiss291323003 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.