An open API service indexing awesome lists of open source software.

https://github.com/cryptiklemur/view-model-bundle

Bundle to add MVVM capabilities to Symfony2
https://github.com/cryptiklemur/view-model-bundle

Last synced: about 1 year ago
JSON representation

Bundle to add MVVM capabilities to Symfony2

Awesome Lists containing this project

README

          

view-model-bundle
=================

Bundle to add View Model capabilities to Symfony2

# This bundle is not receiving any more support.

## Requirements

Requires

* [composer](http://www.getcomposer.org/)
* `symfony/symfony >=2.3.0`

## Installation

In your project root:

```sh
composer require aequasi/view-model-bundle ~4.0.0
```

In your `app/AppKernel.php`

```php
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`][1].
There are also two classes in that namespace that extend the abstract class:

* [`HtmlViewModel`][2] - Sets a content type of `text/html` and doesnt do anything fancy to build the view
* [`JsonViewModel`][3] - Sets a content type of `application/json` and doesnt do anything fancy to build the view

Example:

```php
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][6] and the create method must return a [ViewModelInterface][0].

[0]: https://github.com/aequasi/view-model-bundle/blob/master/src/Aequasi/Bundle/ViewModelBundle/View/Model/ViewModelInterface.php
[1]: https://github.com/aequasi/view-model-bundle/blob/master/src/Aequasi/Bundle/ViewModelBundle/View/Model/AbstractViewModel.php
[2]: https://github.com/aequasi/view-model-bundle/blob/master/src/Aequasi/Bundle/ViewModelBundle/View/Model/HtmlViewModel.php
[3]: https://github.com/aequasi/view-model-bundle/blob/master/src/Aequasi/Bundle/ViewModelBundle/View/Model/JsonViewModel.php
[4]: https://github.com/aequasi/view-model-bundle/blob/master/src/Aequasi/Bundle/ViewModelBundle/Annotation/ViewModel.php
[5]: https://github.com/aequasi/view-model-bundle/blob/master/src/Aequasi/Bundle/ViewModelBundle/Annotation/ViewModelFactory.php
[6]: https://github.com/aequasi/view-model-bundle/blob/master/src/Aequasi/Bundle/ViewModelBundle/Factory/ViewModelFactoryInterface.php