2017 © Pedro Peláez
 

symfony-bundle simple-response-bundle

A Symfony bundle to return simple reponses in your controllers

image

solidworx/simple-response-bundle

A Symfony bundle to return simple reponses in your controllers

  • Sunday, May 20, 2018
  • by pierredup
  • Repository
  • 2 Watchers
  • 1 Stars
  • 14 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 17 % Grown

The README.md

Simple Response Bundle

SimpleResponseBundle is a bundle for the Symfony framework which allows you to return customised response classes in your controllers/actions which reduces the amount of dependencies you controller or action needs., (*1)

Installation

To install the bundle using composer, run the following command:, (*2)

$ composer require solidworx/simple-response-handler

After you have installed the bundle, then you need to register the bundle in your application, (*3)

<?php

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = [
            ...
            new SolidWorx\SimpleResponseBundle\SimpleResponseBundle(),
            ...
        ];

        ...        
     }
}

Usage

This bundle comes with some pre-defined handlers:, (*4)

  • TemplateResponse
  • RouteRedirectResponse

TemplateResponse

The TemplateResponse class will render a template based on the arguments to the class. To render a template, just return an instance of the TemplateResponse class in your route action:, (*5)

<?php

// src/AppBundle/Action/MyAction.php

use SolidWorx\SimpleResponseBundle\Response\TemplateResponse;

class MyAction
{
    public function __invoke()
    {
        return new TemplateResponse('index.html.twig');
    }
}

When loading this action, the index.html.twig template will automatically be rendered without the need to include twig as a dependency in your action class., (*6)

RouteRedirectResponse

The RouteRedirectResponse class will redirect to a given route name., (*7)

<?php

// src/AppBundle/Action/MyAction.php

use SolidWorx\SimpleResponseBundle\Response\RouteRedirectResponse;

class MyAction
{
    public function __invoke()
    {
        return new RouteRedirectResponse('_some_route_name');
    }
}

When loading this action, the page will redirect to the _some_route_name route without the need to include the router in your action or generate the URL., (*8)

Registering custom handlers

To register a custom handler, you need to create a new service that has the response_handler.handler tag. This class needs to implement the SolidWorx\SimpleResponseBundle\ResponseHandlerInterface interface, (*9)

services:
    My\Custom\Handler:
        arguments: ['@doctrine.orm.entity_manager']
        tags: ['solidworx.response_handler']

You then need to create a class that will be used as the return value in your action, (*10)

<?php
use Symfony\Component\HttpFoundation\JsonResponse;

class DoctrineEntityResponse extends JsonResponse
{
    private $entity;

    public function __construct(string $entity)
    {
        $this->entity = $entity;
        parent::__construct();
    }

    public function getEntity(): string
    {
        return $this->entity;
    }
}      

Your handler class will add the logic to return a response object;, (*11)

<?php
use SolidWorx\SimpleResponseBundle\ResponseHandlerInterface;
use Symfony\Component\HttpFoundation\Response;

class Handler implements ResponseHandlerInterface
{
    private $em;

    public function __construct($entityManager)
    {
        $this->em = $entityManager;
    }

    public function supports(Response $object): bool
    {
        return $object instanceof DoctrineEntityReponse; // Only support responses of this type
    }

    public function handle(Response $object): Response
    {
        return $object->setData($this->em->getRepository($object->getEntity())->findAll()); // Return all records in the entity as a JSON response
    }
}

Then you can use your new class in your action:, (*12)

<?php

// src/AppBundle/Action/MyAction.php

class MyAction
{
    public function __invoke()
    {
        return new DoctrineEntityResponse(\App\Entity\Order::class); // Pass the Order entity which will return all orders in a JSON response
    }
}

The Versions

20/05 2018

dev-master

9999999-dev

A Symfony bundle to return simple reponses in your controllers

  Sources   Download

MIT

The Requires

 

The Development Requires

by Pierre du Plessis

20/05 2018

0.2.0

0.2.0.0

A Symfony bundle to return simple reponses in your controllers

  Sources   Download

MIT

The Requires

 

The Development Requires

by Pierre du Plessis

17/03 2018

0.1.0

0.1.0.0

A Symfony bundle to return simple reponses in your controllers

  Sources   Download

MIT

The Requires

 

The Development Requires

by Pierre du Plessis