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
- Host: GitHub
- URL: https://github.com/cryptiklemur/view-model-bundle
- Owner: cryptiklemur
- Created: 2014-01-15T05:17:29.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2017-10-12T20:15:19.000Z (over 8 years ago)
- Last Synced: 2025-03-28T09:01:40.434Z (about 1 year ago)
- Language: PHP
- Size: 26.4 KB
- Stars: 10
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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