定制 alcalyn/serializable-api-response 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

alcalyn/serializable-api-response

Composer 安装命令:

composer require alcalyn/serializable-api-response

包简介

Allows to return raw object as controller response so that it will be serialized later by serializer and converted to Symfony Response.

README 文档

README

If you use Symfony Http foundation and JMS Serializer, this library allows to return raw objects and status code as a controller response so that it will be serialized later by serializer and converted to a Symfony Response.

The problem: Symfony converts response content to string if we use Symfony Response, or if we return a raw object and use a filter listener, then we can't pass status code.

Installation

Install via composer

{
    "require": {
        "alcalyn/serializable-api-response": "~1.0"
    }
}

Usage

Return a non-yet-serialized response in your controller:

use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Alcalyn\SerializableApiResponse\ApiResponse;
use Acme\UserApi\Exception\UserAlreadyExistsException;

class UserController
{
    /**
     * @param Request $request
     *
     * @return ApiResponse
     *
     * @throws ConflictHttpException if username already exists.
     */
    public function registerUserAction(Request $request)
    {
        $username = $request->request->get('username');
        $password = $request->request->get('password');

        try {
            $user = $this->userManager->createUser($username, $password);
        } catch (UserAlreadyExistsException $e) {
            throw new ConflictHttpException('An user with username "'.$username.'" already exists.', $e);
        }

        return new ApiResponse($user, Response::HTTP_CREATED);
    }
}

Register ApiResponse Filter Listener:

In Silex:

use Symfony\Component\HttpKernel\KernelEvents;
use Alcalyn\SerializableApiResponse\ApiResponseFilter;

// Register reponse filter as a service
$this['acme.listener.api_response_filter'] = function () {
    $serializer = $this['serializer']; // Assuming your serializer service has this name

    return new ApiResponseFilter($serializer);
};

// Listen Kernel response to convert ApiResponse with raw object to Symfony Response with serialized data
$this->on(KernelEvents::VIEW, function ($event) {
    $this['acme.listener.api_response_filter']->onKernelView($event);
});

License

This project is under MIT Lisense

统计信息

  • 总下载量: 447
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-05-30

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固