Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Response Content Negotiation Bundle Laravel Package

gisostallenberg/response-content-negotiation-bundle

View on GitHub
Deep Wiki
Context7

Latest Stable Version Latest Unstable Version Total Downloads License

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']])
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
calmfox/watch-sylius
damienfern/grpc-symfony-bundle
atoolo/index-bundle
atoolo/genai-bundle
coprotoai/laravel-ticket
davidjln/llm-carbon-bundle
cryonighter/valid-request-bundle
coolms/taxonomy-bundle
coolms/field-bundle
articulate-orm/symfony
aaix/laravel-tall-architect
ephoto/akeneo-connector
emmanuelballery/eb-plantumlbundle
emielburgman/symfony-visitor-beacon
emielburgman/symfony-visit-storage
emielburgman/symfony-security-headers
emielburgman/symfony-log-viewer
emarref/xdebug-bundle
emarref/pubnub-bundle
elriseio/finance-money-bundle