2017 © Pedro Peláez
 

symfony-bundle view-bundle

mvrhov view bundle

image

mvrhov/view-bundle

mvrhov view bundle

  • Wednesday, December 27, 2017
  • by mvrhov
  • Repository
  • 1 Watchers
  • 1 Stars
  • 13 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

View bundle for Symfony

This bundle adds two View classes that you can return from your controller's action RouteView, TemplateView and an interface ResponderInterface which the class you are returning from the controller should implement., (*1)

Installation

  1. Require View bundle as a dependency using Composer:, (*2)

    php composer.phar require mvrhov/view-bundle
    
  2. Add bundle to app/AppKernel.php, (*3)

       public function registerBundles()
       {
           return array(
               new \mvrhov\ViewBundle\mvrhovViewBundle();
               // ...
           );
       }
    
  3. You are done., (*4)

Examples

RouteView

use mvrhov\ViewBundle\View\RouteView;
use mvrhov\ViewBundle\View\ViewInterface;

final class RouteAction
{
    public function __invoke(): ViewInterface
    {
        $params = [
            'param1' => 'view',
            'param2' => 'bundle',
        ];

        return new RouteView('my_route', $params);
    }
}

TemplateView

use mvrhov\ViewBundle\View\TemplateView;
use mvrhov\ViewBundle\View\ViewInterface;

final class TemplateAction
{
    public function __invoke(): ViewInterface
    {
        $data = [
            'foo' => 1,
            'bar' => 'yep'
        ];

        return new TemplateView('@Bundle/template.html.twig', $data);
    }
}

Responder

use mvrhov\ViewBundle\View\ResponderInterface;
use mvrhov\ViewBundle\View\TemplateView;
use mvrhov\ViewBundle\View\RouteView;
use mvrhov\ViewBundle\View\ResponseView;

final class InvoiceResponder implements ResponderInterface;
{
    private $invoices;

    public function __construct(array $invoices)
    {
        $this->invoices = $invoices;
    }

    public function getView(Request $request, int $requestType): ViewInterface
    {
        if ('application/json' !== $request->getContentType()) {
            $total = count($this->invoices);

            if (0 === $total) {
                return new RouteView('list_invoices');
            }

            if (1 === $total) {
                return new TemplateView('@Bundle/template_one.html.twig', $this->invoices);
            }

            if (5 > $total) {
                return new TemplateView('@Bundle/template_a_lot.html.twig', $this->invoices);
            }
        } else {
            return new ResponseView(new Response(json_serialize($this->invoices)));
        }
    }
}


use mvrhov\ViewBundle\View\TemplateView;
use mvrhov\ViewBundle\View\ResponderInterface;

final class ResponderAction
{
    public function __invoke(): ResponderInterface
    {
        $invoices = $this->getInvoices();

        return new InvoiceResponder($invoices);
    }
}

The Versions

27/12 2017

dev-master

9999999-dev

mvrhov view bundle

  Sources   Download

MIT

The Requires

 

view bundle