Coder Social home page Coder Social logo

Comments (5)

UphkcRNqmafQWcSSSSSS avatar UphkcRNqmafQWcSSSSSS commented on June 14, 2024
<?php

namespace App\Support;

class ResponseFormat extends \Jiannei\Response\Laravel\Support\Format
{
  /**
   * Format return data structure.
   *
   * @param  array|null  $data
   * @param  string|null  $message
   * @param  int  $code
   * @param  null  $errors
   * @return array
   */
  public function data(?array $data, ?string $message, int $code, $errors = null): array
  {
    return $this->formatDataFields([
      'code' => $code,
      'message' => $this->formatMessage($code, $message),
      'data' => $data ?: (object) $data,
      'error' => $errors ?: (object) [],
      'status' => $this->formatStatus($code),
      'timestamp' => (string) now()->getTimestampMs(),
    ], $this->config);
  }
}

// config/response.php
// Set the structure of the response data
  'format' => [
    // \Jiannei\Response\Laravel\Support\Format::class,
    \App\Support\ResponseFormat::class,
    [
      'code' => ['alias' => 'code', 'show' => true],
      'status' => ['alias' => 'status', 'show' => true],
      'message' => ['alias' => 'message', 'show' => true],
      //....
      'error' => ['alias' => 'error', 'show' => false],
      'timestamp' => ['alias' => 'timestamp', 'show' => true],
    ],

from laravel-response.

lisongkun avatar lisongkun commented on June 14, 2024
<?php

namespace App\Support;

class ResponseFormat extends \Jiannei\Response\Laravel\Support\Format
{
  /**
   * Format return data structure.
   *
   * @param  array|null  $data
   * @param  string|null  $message
   * @param  int  $code
   * @param  null  $errors
   * @return array
   */
  public function data(?array $data, ?string $message, int $code, $errors = null): array
  {
    return $this->formatDataFields([
      'code' => $code,
      'message' => $this->formatMessage($code, $message),
      'data' => $data ?: (object) $data,
      'error' => $errors ?: (object) [],
      'status' => $this->formatStatus($code),
      'timestamp' => (string) now()->getTimestampMs(),
    ], $this->config);
  }
}

// config/response.php
// Set the structure of the response data
  'format' => [
    // \Jiannei\Response\Laravel\Support\Format::class,
    \App\Support\ResponseFormat::class,
    [
      'code' => ['alias' => 'code', 'show' => true],
      'status' => ['alias' => 'status', 'show' => true],
      'message' => ['alias' => 'message', 'show' => true],
      //....
      'error' => ['alias' => 'error', 'show' => false],
      'timestamp' => ['alias' => 'timestamp', 'show' => true],
    ],

一样的,我用的Laravel10的原因吗

from laravel-response.

jiannei avatar jiannei commented on June 14, 2024

问题解决了吗 🤔

from laravel-response.

lisongkun avatar lisongkun commented on June 14, 2024

问题解决了吗 🤔

还没有呢,Laravel10 里不能用原先的解决方案,勉强用中间件替代

from laravel-response.

jiannei avatar jiannei commented on June 14, 2024

可以参考测试用例看看:

from laravel-response.

Related Issues (20)

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.