PHP code example of aequasi / view-model-bundle

1. Go to this page and download the library: Download aequasi/view-model-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

aequasi / view-model-bundle example snippets


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



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
  }
}


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;
  }
}