{"id":17347543,"url":"https://github.com/cryptiklemur/view-model-bundle","last_synced_at":"2025-04-14T20:43:55.942Z","repository":{"id":13240955,"uuid":"15925716","full_name":"cryptiklemur/view-model-bundle","owner":"cryptiklemur","description":"Bundle to add MVVM capabilities to Symfony2","archived":false,"fork":false,"pushed_at":"2017-10-12T20:15:19.000Z","size":27,"stargazers_count":10,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-28T09:01:40.434Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cryptiklemur.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-01-15T05:17:29.000Z","updated_at":"2021-06-13T08:40:10.000Z","dependencies_parsed_at":"2022-08-25T17:40:45.131Z","dependency_job_id":null,"html_url":"https://github.com/cryptiklemur/view-model-bundle","commit_stats":null,"previous_names":["cryptiklemur/view-model-bundle","aequasi/view-model-bundle"],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cryptiklemur%2Fview-model-bundle","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cryptiklemur%2Fview-model-bundle/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cryptiklemur%2Fview-model-bundle/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cryptiklemur%2Fview-model-bundle/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cryptiklemur","download_url":"https://codeload.github.com/cryptiklemur/view-model-bundle/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248960209,"owners_count":21189979,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-15T16:49:09.640Z","updated_at":"2025-04-14T20:43:55.926Z","avatar_url":"https://github.com/cryptiklemur.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"view-model-bundle\n=================\n\nBundle to add View Model capabilities to Symfony2\n\n# This bundle is not receiving any more support.    \n\n## Requirements\n\nRequires \n\n* [composer](http://www.getcomposer.org/)\n* `symfony/symfony \u003e=2.3.0`\n\n## Installation\n\nIn your project root:\n\n```sh\ncomposer require aequasi/view-model-bundle ~4.0.0\n```\n\nIn your `app/AppKernel.php`\n\n```php\npublic function registerBundles()\n{\n    $bundles = array(\n        // ...\n        new Aequasi\\Bundle\\ViewModelBundle\\AequasiViewModelBundle(),\n    );\n    // ...\n}\n```\n\n## Usage\n\nTo make a view model, create a new class that implements the [`ViewModelInterface`][0]. \nThere are some other classes in that namespace that make it easy to make your own view model. I heavily suggest extending the [`AbstractViewModel`][1].\nThere are also two classes in that namespace that extend the abstract class:\n\n* [`HtmlViewModel`][2] - Sets a content type of `text/html` and doesnt do anything fancy to build the view\n* [`JsonViewModel`][3] - Sets a content type of `application/json` and doesnt do anything fancy to build the view\n\nExample:\n\n```php\n\u003c?php\n\nnamespace Acme\\DemoBundle\\View\\Index;\n\nuse Aequasi\\Bundle\\ViewModelBundle\\View\\Model\\HtmlViewModel;\n\nclass IndexViewModel extends HtmlViewModel\n{\n  protected $template = 'AcmeDemoBundle:Index:index.html.twig';\n\n  public function buildView($data)\n  {\n    // Do some stuff with data, and then return what you want in the view\n    return $data; // Does this by default in the ViewModel\n  }\n}\n```\n\nYou 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.\n\n#### To Use the View Model\n\nUse the [`@ViewModel`][4] Annotation in your action OR you can use the [`@ViewModelFactory`][5] Annotation:\n\n```php\n\nnamespace Acme\\DemoBundle\\Controller;\n\nuse Aequasi\\Bundle\\ViewModelBundle\\Annotation\\ViewModel;\nuse Aequasi\\Bundle\\ViewModelBundle\\Service\\ViewModelService;\nuse Aequasi\\Bundle\\ViewModelBundle\\Controller\\ViewModelControllerInterface;\n\nclass IndexController\n{\n\n  /**\n   * @var ViewModelService\n   */\n  private $view;\n\n  /**\n   * @ViewModel(\"Acme\\DemoBundle\\View\\Model\\Index\\IndexViewModel\")\n   * OR\n   * @ViewModel(\"@some.service.name\")\n   * @ViewModelFactory(\"@service.id\", {\"argumentOne\", \"argumentTwo\"}) // You can also use a class name. The arguments are for you to decide what view model to use\n   */ \n  public function indexAction()\n  {\n    $this-\u003eview = $this-\u003econtainer-\u003eget('aequasi.view_model.service.view');\n    \n    $this-\u003egetView()-\u003eadd('someParameter', 'someValue');\n    return $this-\u003egetView()-\u003erender(/*$templatName, $response*/);\n    \n    // You can also not return anything and it will create the response for you\n    // It will also let you return an array that gets set as your view parameters\n    return array('someParameter', 'someValue');\n  }\n  \n  public function setView(ViewModelService $service)\n  {\n    $this-\u003eview = $service;\n  }\n  \n  public function getView()\n  {\n    return $this-\u003eview;\n  }\n}\n```\n\n###### A ViewModelFactory must implement [ViewModelFactoryInterface][6] and the create method must return a [ViewModelInterface][0].\n\n[0]: https://github.com/aequasi/view-model-bundle/blob/master/src/Aequasi/Bundle/ViewModelBundle/View/Model/ViewModelInterface.php\n[1]: https://github.com/aequasi/view-model-bundle/blob/master/src/Aequasi/Bundle/ViewModelBundle/View/Model/AbstractViewModel.php\n[2]: https://github.com/aequasi/view-model-bundle/blob/master/src/Aequasi/Bundle/ViewModelBundle/View/Model/HtmlViewModel.php\n[3]: https://github.com/aequasi/view-model-bundle/blob/master/src/Aequasi/Bundle/ViewModelBundle/View/Model/JsonViewModel.php\n[4]: https://github.com/aequasi/view-model-bundle/blob/master/src/Aequasi/Bundle/ViewModelBundle/Annotation/ViewModel.php\n[5]: https://github.com/aequasi/view-model-bundle/blob/master/src/Aequasi/Bundle/ViewModelBundle/Annotation/ViewModelFactory.php\n[6]: https://github.com/aequasi/view-model-bundle/blob/master/src/Aequasi/Bundle/ViewModelBundle/Factory/ViewModelFactoryInterface.php\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcryptiklemur%2Fview-model-bundle","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcryptiklemur%2Fview-model-bundle","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcryptiklemur%2Fview-model-bundle/lists"}