2017 © Pedro Peláez
 

library view-model-bundle

Bundle to add ViewModel capabilities to Symfony2

image

aequasi/view-model-bundle

Bundle to add ViewModel capabilities to Symfony2

  • Thursday, October 12, 2017
  • by aequasi
  • Repository
  • 4 Watchers
  • 10 Stars
  • 4,958 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 12 Versions
  • 0 % Grown

The README.md

view-model-bundle

Bundle to add View Model capabilities to Symfony2, (*1)

This bundle is not receiving any more support.

Requirements

Requires, (*2)

Installation

In your project root:, (*3)

composer require aequasi/view-model-bundle ~4.0.0

In your app/AppKernel.php, (*4)

public function registerBundles()
{
    $bundles = array(
        // ...
        new Aequasi\Bundle\ViewModelBundle\AequasiViewModelBundle(),
    );
    // ...
}

Usage

To make a view model, create a new class that implements the [ViewModelInterface]0. There are some other classes in that namespace that make it easy to make your own view model. I heavily suggest extending the AbstractViewModel. There are also two classes in that namespace that extend the abstract class:, (*5)

  • HtmlViewModel - Sets a content type of text/html and doesnt do anything fancy to build the view
  • JsonViewModel - Sets a content type of application/json and doesnt do anything fancy to build the view

Example:, (*6)

<?php

namespace Acme\DemoBundle\View\Index;

use Aequasi\Bundle\ViewModelBundle\View\Model\HtmlViewModel;

class IndexViewModel extends HtmlViewModel
{
  protected $template = 'AcmeDemoBundle:Index:index.html.twig';

  public function buildView($data)
  {
    // Do some stuff with data, and then return what you want in the view
    return $data; // Does this by default in the ViewModel
  }
}

You don't have to follow the same namespace structure, but it allows for a cleaner structure. This view model would be the view model for the IndexController's indexAction., (*7)

To Use the View Model

Use the @ViewModel Annotation in your action OR you can use the @ViewModelFactory Annotation:, (*8)


namespace Acme\DemoBundle\Controller; use Aequasi\Bundle\ViewModelBundle\Annotation\ViewModel; use Aequasi\Bundle\ViewModelBundle\Service\ViewModelService; use Aequasi\Bundle\ViewModelBundle\Controller\ViewModelControllerInterface; class IndexController { /** * @var ViewModelService */ private $view; /** * @ViewModel("Acme\DemoBundle\View\Model\Index\IndexViewModel") * OR * @ViewModel("@some.service.name") * @ViewModelFactory("@service.id", {"argumentOne", "argumentTwo"}) // You can also use a class name. The arguments are for you to decide what view model to use */ public function indexAction() { $this->view = $this->container->get('aequasi.view_model.service.view'); $this->getView()->add('someParameter', 'someValue'); return $this->getView()->render(/*$templatName, $response*/); // You can also not return anything and it will create the response for you // It will also let you return an array that gets set as your view parameters return array('someParameter', 'someValue'); } public function setView(ViewModelService $service) { $this->view = $service; } public function getView() { return $this->view; } }
A ViewModelFactory must implement ViewModelFactoryInterface and the create method must return a [ViewModelInterface]0.

The Versions

12/10 2017

dev-master

9999999-dev

Bundle to add ViewModel capabilities to Symfony2

  Sources   Download

MIT

The Requires

 

12/10 2017

dev-aequasi-patch-1

dev-aequasi-patch-1

Bundle to add ViewModel capabilities to Symfony2

  Sources   Download

MIT

The Requires

 

07/11 2016

1.1.1

1.1.1.0

Bundle to add ViewModel capabilities to Symfony2

  Sources   Download

MIT

The Requires

 

07/11 2016

4.0.2

4.0.2.0

Bundle to add ViewModel capabilities to Symfony2

  Sources   Download

MIT

The Requires

 

14/12 2014

4.0.1

4.0.1.0

Bundle to add ViewModel capabilities to Symfony2

  Sources   Download

MIT

The Requires

 

14/12 2014

4.0.0

4.0.0.0

Bundle to add ViewModel capabilities to Symfony2

  Sources   Download

MIT

The Requires

 

13/12 2014

3.0.0

3.0.0.0

Bundle to add ViewModel capabilities to Symfony2

  Sources   Download

MIT

The Requires

 

21/11 2014

2.0.2

2.0.2.0

Bundle to add ViewModel capabilities to Symfony2

  Sources   Download

MIT

The Requires

 

21/11 2014

2.0.1

2.0.1.0

Bundle to add ViewModel capabilities to Symfony2

  Sources   Download

MIT

The Requires

 

21/11 2014

2.0.0

2.0.0.0

Bundle to add ViewModel capabilities to Symfony2

  Sources   Download

MIT

The Requires

 

24/01 2014

1.1.0

1.1.0.0

Bundle to add ViewModel capabilities to Symfony2

  Sources   Download

MIT

The Requires

 

24/01 2014

1.0.0

1.0.0.0

Bundle to add ViewModel capabilities to Symfony2

  Sources   Download

MIT

The Requires