venoudev/results

一个用于在Laravel或Lumen项目中构建非常易于理解的JSON结构响应的绝佳包,适用于您的API Rest。

0.3.2 2022-04-01 03:28 UTC

README

Venou Dev

VenouDev Results

一个用于在Laravel或Lumen项目中构建非常易于理解的JSON结构响应的绝佳包,适用于您的API Rest。

安装

  composer require venoudev/results

Lumen注册提供者

在你的bootstrap/app.php文件中配置

  /**
  * Venoudev Results
  */
  $app->register(Venoudev\Results\Providers\LumenResultsServiceProvider::class);
  

资源安装

Laravel

安装包的资源,并显示一个绝佳的信息

 php artisan results:install-resources

如果你没有看到绝佳的信息,使用

  php artisan vendor:publish --tag=results-resources

Laravel

更新你的config/app.php文件,使用以下内容。

  ...
  /*
    |--------------------------------------------------------------------------
    | Application Locale Configuration
    |--------------------------------------------------------------------------
    |
    | The application locale determines the default locale that will be used
    | by the translation service provider. You are free to set this value
    | to any of the locales which will be supported by the application.
    |
    */
  'locale' => env('APP_LOCALE', 'en'),
  ...

Lumen

  mkdir resources/lang
  cp -R vendor/venoudev/results/resources/lang/en_results_venoudev resources/lang

在config/app.php中创建文件(如果你没有看到config文件夹,创建这个文件夹)

  <?php

    return [

        /*
        |--------------------------------------------------------------------------
        | Application Name
        |--------------------------------------------------------------------------
        |
        | This value is the name of your application. This value is used when the
        | framework needs to place the application's name in a notification or
        | any other location as required by the application or its packages.
        |
        */

        'name' => env('APP_NAME', 'product-service'),

        /*
        |--------------------------------------------------------------------------
        | Application Environment
        |--------------------------------------------------------------------------
        |
        | This value determines the "environment" your application is currently
        | running in. This may determine how you prefer to configure various
        | services the application utilizes. Set this in your ".env" file.
        |
        */

        'env' => env('APP_ENV', 'production'),

        /*
        |--------------------------------------------------------------------------
        | Application Debug Mode
        |--------------------------------------------------------------------------
        |
        | When your application is in debug mode, detailed error messages with
        | stack traces will be shown on every error that occurs within your
        | application. If disabled, a simple generic error page is shown.
        |
        */

        'debug' => env('APP_DEBUG', false),

        /*
        |--------------------------------------------------------------------------
        | Application URL
        |--------------------------------------------------------------------------
        |
        | This URL is used by the console to properly generate URLs when using
        | the Artisan command line tool. You should set this to the root of
        | your application so that it is used when running Artisan tasks.
        |
        */

        'url' => env('APP_URL', 'https://'),

        /*
        |--------------------------------------------------------------------------
        | Application Timezone
        |--------------------------------------------------------------------------
        |
        | Here you may specify the default timezone for your application, which
        | will be used by the PHP date and date-time functions. We have gone
        | ahead and set this to a sensible default for you out of the box.
        |
        */

        'timezone' => 'UTC',

        /*
        |--------------------------------------------------------------------------
        | Application Locale Configuration
        |--------------------------------------------------------------------------
        |
        | The application locale determines the default locale that will be used
        | by the translation service provider. You are free to set this value
        | to any of the locales which will be supported by the application.
        |
        */

        'locale' => env('APP_LOCALE', 'en'),

        /*
        |--------------------------------------------------------------------------
        | Application Fallback Locale
        |--------------------------------------------------------------------------
        |
        | The fallback locale determines the locale to use when the current one
        | is not available. You may change the value to correspond to any of
        | the language folders that are provided through your application.
        |
        */

        'fallback_locale' => env('APP_FALLBACK_LOCALE', 'en'),

        /*
        |--------------------------------------------------------------------------
        | Encryption Key
        |--------------------------------------------------------------------------
        |
        | This key is used by the Illuminate encrypter service and should be set
        | to a random, 32 character string, otherwise these encrypted strings
        | will not be safe. Please do this before deploying an application!
        |
        */

        'key' => env('APP_KEY'),

        'cipher' => 'AES-256-CBC',

    ];

Laravel和Lumen

然后在.env文件中定义

  APP_LOCALE=en_results_venoudev

并在控制台执行

  php artisan config:clear

如果遇到ResultManager未找到的问题,请在Laravel中配置Facade ResultManager

在你的app/config/app.php中,在aliases数组中添加以下代码

  'aliases' => [

      'App' => Illuminate\Support\Facades\App::class,
      'Arr' => Illuminate\Support\Arr::class,
      'Artisan' => Illuminate\Support\Facades\Artisan::class,
       
       //.... more alisases

      /*
      * Custom Aliases
      */

      'ResultManager' => Venoudev\Results\Facades\ResultManagerFacade::class,
  ],
  

Lumen Facade

在bootstrap/app.php中创建一个数组,包含你的外观,并包含ResultManager Facade

 
  $facades = [
      '\Venoudev\Results\Facades\ResultManagerFacade' =>  'ResultManager',
      'Illuminate\Support\Facades\App' => 'App'
  ];

  $app->withFacades(true ,$facades);
  $app->withEloquent();

 

在Lumen和Swoole中使用Results

在config/swoole_http.php中为每个请求注册Lumen服务提供者。这是必要的,以便Lumen在Swoole服务器中使用时可以找到Facade ResultManager。

```
  /*
|--------------------------------------------------------------------------
| Providers here will be registered on every request.
|--------------------------------------------------------------------------
*/
'providers' => [
    Illuminate\Pagination\PaginationServiceProvider::class,
    Venoudev\Results\Providers\LumenResultsServiceProvider::class,
],

```

用于创建组件的命令

  1. 生成一个骨架验证器
  php artisan make:validator Example

generate =>

    <?php

    namespace App\Validators;

    use Illuminate\Support\Facades\Validator;
    use Venoudev\Results\Exceptions\CheckDataException;

    class ExampleValidator
    {

        public static function execute($data){

            $validator=Validator::make($data,[
                 // Your awesome laravel validations here
            ]);

            if ($validator->fails()) {
                $exception = new CheckDataException();
                $exception->addFieldErrors($validator->errors());
                throw $exception;
            }
            return;
        }
    }
  1. 生成一个骨架ActionClass
  php artisan make:action Example

generate =>

    <?php

    namespace App\Actions;

    class ExampleAction
    {

        public static function execute($data){
          // Your awesome code here.
          return;
        }
    }
  1. 生成两个骨架,第一个是一个Contract,第二个是从这些Contract中实现的类
  php artisan make:service Example

generate =>

Contract

    <?php

    namespace App\Services\Contracts;

    interface ExampleService {
         // Your awesome methods to implement here.
    }

Implementation

    <?php

    namespace App\Services;

    use App\Services\Contracts\ExampleService;
    use Illuminate\Http\Request;

    class ExampleServiceImpl implements ExampleService{

        // Your awesome implemented methods here.

    }

在你的ServiceProvider或自定义Provider的boot方法中,将contract绑定到implementation,并在控制器中使用contract,例如通过依赖注入

app\Providers\AppServiceProvider.php

    use App\Services\ExampleServiceImpl;
    use App\Services\Contracts\ExampleService;

    ...

    public function boot()
    {
        $this->app->bind(ExampleService::class,ExampleImpl::class);
    }

Venoudev/Results的JSON响应结构

{
    "success": "bool",
    "description": "string",
    "data": { "dynamic" },
    "errors": [
        {
            "error_code": "CODE",
            "field": "field || NOTHING",
            "message": "string"
        }
    ],
    "messages": [
        {
            "message_code": "CODE",
            "message": "string"
        }
    ]
}

错误消息

示例

    {
        "message_code": "LOGIN_SUCCESS",
        "message": "Login do correctly"
    }

错误对象

示例

    {
        "error_code": "REQUIRED",
        "field": "email",
        "message": "The email field is required."
    }
    {
        "error_code": "STATUS_USER",
        "field": "NOTHING",
        "message": "The status of user is baned."
    }

示例

代码 200 Ok

内容

{
    "success": true,
    "description": "Welcome Be Awesome!",
    "data": {
        "access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOiIxIiwianRpIjoiMjAxZWIzN2IyMTVkNTc5OWU0ZDg2NjJlNTRkYWM5OWEyNTYzMWE3OWM4MWZiMGEzNmRkMDY3NzdlN2M3ZTllZjYzMTA1ZjNiZmYwMzgxOWQiLCJpYXQiOjE1OTI2OTU4ODYsIm5iZiI6MTU5MjY5NTg4NiwiZXhwIjoxNjI0MjMxODg2LCJzdWIiOiIxIiwic2NvcGVzIjpbXX0.JgK84-CDMJ6xOQlmpnSdvtTGu0to0mDi0tjY6JhZCeGrfWSWb23SEf3rNDKbtWWiSAp3yBnP08v9J9GYrMwtx9ItYoANGn8qjSr2GVep2bK9GjjkErOkDWOIXeEw7tPxD5KD4xWXKY6_uiGX3Jj5m6EbhFsxzj1q1nIpJtGBxkVNQvg1fDtUGjc2qA5aFiqjRGDajFTPMyojyTOvf-tKhid_RWupdz5H4fBBjODMCAw4XBmqRhvT6WHv0WWAyvwoJCzAQTWqiEpctqthc-0HzpGTBxuqsdj71poowFJMtnL6r6_AYsEOn2IrDsR8tNjIBQ05iDrM6KZkHTuHVsKPo7augrwf6glpsuiASuy4Au1VlwJVUfno3xjCTcX7vsNzvqVSb6E2_0FWTTMwSHqkWQQNfI03daDOFyVej69U_4DqbN_cvcl9rZJp-WYXiB3C89Za1UwSxp8Ff9xcYowrw8vwb0PHvnPpkMTeHAnS59zLQrl5R-fqh-PKJe0gACK3W5-weJqoyE7_B-FziFqZdRhm7zwvSEZW2myEFdNOiUBeJ7OUV81a5CP1Gt7C0n5ejQhoPN5s60qHcSiYQFaKuhI_6rWLW9bNlFSqzHTA1DHYFFBQg4j6Vx-EqaAuZGw_cCYZMpF95A8C9_kLtjh1ayHhKae773BCulf_1ZEAYE",
        "roles": [
            "admin"
        ]
    },
    "errors": [],
    "messages": [
        {
            "message_code": "LOGIN_SUCCESS",
            "message": "login do correctly"
        }
    ]
}

代码 400 Bad Request

内容


{
    "success": false,
    "description": "exist conflict whit the request, please check the errors and messages",
    "data": [],
    "errors": [],
    "messages": [
        {
            "message_code": "FAILED_AUTH",
            "message": "Invalid login credential"
        }
    ]
}

代码 400 Bad Request

内容

{
    "success": false,
    "description": "exist conflict whit the request, please check the errors and messages",
    "data": [],
    "errors": [
        {
            "error_code": "REQUIRED",
            "field": "email",
            "message": "The email field is required."
        },
        {
            "error_code": "REQUIRED",
            "field": "password",
            "message": "The password field is required."
        }
    ],
    "messages": [
        {
            "message_code": "CHECK_DATA",
            "message": "The form has errors whit the inputs"
        }
    ]
}

网站

https://venoudev.com