gisostallenberg / response-content-negotiation-bundle
A bundle that allows creating various Response types from a controller, based on content negotiation
Installs: 13 954
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 3
Forks: 3
Open Issues: 3
Requires
- php: >=7.3
- friendsofsymfony/rest-bundle: ^3.0
- symfony/config: ^6.0
- symfony/contracts: ^3.0
- symfony/dependency-injection: ^6.0
- symfony/event-dispatcher: ^6.0
- symfony/http-foundation: ^6.0
- symfony/http-kernel: ^6.0
- symfony/twig-bundle: ^6.0
- twig/twig: ^3.0
- willdurand/negotiation: ^3.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.6
- jms/serializer-bundle: ^4.0
- localheinz/composer-normalize: ^2.2
- maglnet/composer-require-checker: ^3.8
- phpstan/phpstan: ^0.12.8
- phpunit/phpunit: ^8.5
- sensiolabs/security-checker: ^6.0
- symfony/browser-kit: ^6.0
- symfony/debug-pack: ^1.0
- symfony/framework-bundle: ^6.0
- dev-master
- 2.x-dev
- 2.0.0
- 1.0.1
- 1.0.0
- 0.9.0
- 0.8.1
- 0.8.0
- 0.7.0
- 0.6.0
- 0.5.4
- 0.5.3
- 0.5.2
- 0.5.1
- 0.5.0
- 0.4.0
- 0.3.0
- 0.2.0
- 0.1.1
- 0.1.0
- dev-dependabot/composer/symfony/http-client-5.4.47
- dev-dependabot/composer/symfony/http-foundation-5.4.46
- dev-dependabot/composer/twig/twig-3.14.1
- dev-dependabot/composer/symfony/http-client-5.4.46
- dev-json-status-code
This package is auto-updated.
Last update: 2024-11-13 16:48:36 UTC
README
Response Content Negotiation Bundle
A bundle that allows creating various Response types from a controller, based on content negotiation
Installation
composer require gisostallenberg/response-content-negotiation-bundle
Requirements
HTML
To render HTML the bundle requires TwigBundle.
JSON & XML
To output JSON or XML the bundle requires FOS Rest Bundle and JMS Serializer Bundle or Symfony Serializer
Usage
<?php namespace App\Controller; use GisoStallenberg\Bundle\ResponseContentNegotiationBundle\Content\ResultData; use GisoStallenberg\Bundle\ResponseContentNegotiationBundle\Content\ResultInterface; use GisoStallenberg\Bundle\ResponseContentNegotiationBundle\Content\ResultServiceLocator; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Annotation\Route; class AcmeController { /** * @Route("/my-page", methods={"GET"}) */ public function resultAction( ResultServiceLocator $resultServiceLocator, Request $request ): ResultInterface { return $resultServiceLocator->getResult($request, new ResultData('acme', ['my-data-label' => 'my data'])); } }
HTML
This will render acme.html.twig
with the Twig context ['my-data-label' => 'my data']
.
If you want to render a specific template, you can add this as template
argument to the ResultData.
For example:
new ResultData('acme', ['my-data-label' => 'my data'], ['template' => '@AcmeBundle/templates/acme.twig.html'])
This will render @AcmeBundle/templates/acme.twig.html
with the Twig context ['my-data-label' => 'my data']
.
JSON
This will respond with
{ "my-data-label": "my data" }
XML
This will respond with
<my-data-label> <entry>my data</entry> <my-data-label>
Serialization
To use serialization groups in the serialization process, you can add the groups
option parameter.
For example:
new ResultData('acme', ['my-data-label' => 'my data'], ['groups' => ['profile', 'list']])