定制 chamber-orchestra/view-bundle 二次开发

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

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

chamber-orchestra/view-bundle

最新稳定版本:v8.0.9

Composer 安装命令:

composer require chamber-orchestra/view-bundle

包简介

The view symfony bundle for JSON API responses

README 文档

README

PHP Composer

ChamberOrchestra View Bundle

A lightweight Symfony bundle for building typed, reusable JSON responses. Views encapsulate serialization concerns so controllers can return simple objects instead of Response.

Requirements

  • PHP 8.4
  • Symfony 8.0 components (http-kernel, serializer, property-access, dependency-injection, config)
  • doctrine/common ^3.5

Installation

composer require chamber-orchestra/view-bundle:8.0.*

Enable the bundle in config/bundles.php:

return [
    // ...
    ChamberOrchestra\ViewBundle\ChamberOrchestraViewBundle::class => ['all' => true],
];

Quickstart

Create a view that maps fields from a domain object:

use ChamberOrchestra\ViewBundle\View\BindView;
use ChamberOrchestra\ViewBundle\Attribute\Type;
use ChamberOrchestra\ViewBundle\View\IterableView;

final class UserView extends BindView
{
    public string $id;
    public string $name;

    #[Type(ImageView::class)]
    public IterableView $images;

    public function __construct(User $user)
    {
        parent::__construct($user);
    }
}

final class ImageView extends BindView
{
    public string $path;
}

Return a view from a controller:

#[Route('/user/me', methods: ['GET'])]
final class GetMeAction
{
    public function __invoke(): UserView
    {
        return new UserView($this->getUser());
    }
}

ViewSubscriber converts any ViewInterface result into a JsonResponse. Non-view results are ignored.

Core Views

  • ResponseView: base response with status (200) and headers (Content-Type: application/json), overridable in subclasses.
  • DataView: wraps payload under data.
  • BindView: maps matching properties from a source object to the view; honors Attribute\Type on IterableView properties for typed collections.
  • IterableView: maps collections via a callback or view class string.
  • KeyValueView: returns an associative array for metadata blocks.

Caching & Build ID

SetVersionSubscriber seeds BindUtils::configure() with container.build_id. When APP_DEBUG=false, property accessor caching is enabled with a 24h lifetime and namespace view_bind.

Development & Tests

  • Install deps: composer install
  • Run unit/integration tests: ./bin/phpunit
  • Namespaces live under ChamberOrchestra\ViewBundle; autoloaded PSR-4 from src/.

统计信息

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

GitHub 信息

  • Stars: 227
  • Watchers: 3
  • Forks: 22
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-12-29