artyuum/symfony-jsend-response

基于HttpFoundation的应用(如Symfony、Silex、Laravel、Drupal等)的JSendResponse组件。它遵循JSend规范,允许您向用户提供一致的JSON响应。

1.2.0 2024-01-21 15:32 UTC

This package is auto-updated.

Last update: 2024-09-22 11:35:27 UTC


README

JSendResponse组件,用于基于HttpFoundation的应用(如Symfony、Silex、Laravel、Drupal等)。它遵循JSend规范,允许您向用户提供一致的JSON响应。

注意:此存储库是Junker/JSendResponse的维护分支。请参阅发布页面以了解修改历史。

需求

  • PHP ^7.4 || ^8.0
  • Symfony ^5.0 || ^6.0 || ^7.0

安装

安装JSendResponse的最佳方式是使用Composer

composer require artyuum/symfony-jsend-response

示例

use Junker\JSendResponse\JSendResponse;
use Junker\JSendResponse\JSendSuccessResponse;
use Junker\JSendResponse\JSendFailResponse;
use Junker\JSendResponse\JSendErrorResponse;


class AppController
{
	...

	$data = ['id' => 50, 'name' => 'Waldemar'];
	$message = 'Error, total error!';
	$code = 5;

	return new JSendResponse(JSendResponse::STATUS_SUCCESS, $data);
	// or
	return new JSendResponse(JSendResponse::STATUS_FAIL, $data);
	// or 
	return new JSendResponse(JSendResponse::STATUS_ERROR, NULL, $message);
	// or
	return new JSendResponse(JSendResponse::STATUS_ERROR, $data, $message, $code);
	// or
	return new JSendSuccessResponse($data);
	// or
	return new JSendFailResponse($data);
	// or
	return new JSendErrorResponse($message);
	// or
	return new JSendErrorResponse($message, $code, $data);

}

贡献

如果您想做出贡献,请将该存储库进行分支,并按照您喜欢的任何方式进行修改。请确保遵循此库中使用的相同的编码风格和命名约定,以生成一致的代码。