PHP code example of olekhy / view-model-service

1. Go to this page and download the library: Download olekhy/view-model-service 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/ */

    

olekhy / view-model-service example snippets



use ViewModelService\ViewModelRepo;

class UserController
{

	function actionShowProfile()
	{
		$self = $this;

		ViewModelRepo::getRepo()->addUserProfile(function() use($self)
		{
			$userId = $self->getRequest()->getParam('userId');
			return $self->getUserService()->findUserProfile($userId);
		);});
	}

	public function getUserService()
	{
		return new UserService();
	}
}



	echo ViewModelRepo::getRepo()->getUserProfile()->firstname;
	echo '<br/>';
	echo ViewModelRepo::getRepo()->getUserProfile()->lastname;



// output immediately as json
echo json_encode(ViewModelRepo::getRepo()->addConcreteViewModel($callable)->getConcreteMode());

// creation of the view model with an specific identifier
// this is necessary for creation more than one view model instance that contains different data
ViewModelRepo::getRepo()->addConcreteViewModel(array('status_first' => 1, 'status_second' => 2, 'level' => 3), 'id_concrete_1');

ViewModelRepo::getRepo()->addConcreteViewModel(new stdClass, 'id_concrete_2');

// get data in template
echo ViewModelRepo::getRepo()->getConcreteViewModel();
// ...
echo ViewModelRepo::getRepo()->getConcreteViewModel('id_concrete_1')->status_first;
// ...
echo ViewModelRepo::getRepo()->getConcreteViewModel('id_concrete_2');