1. Go to this page and download the library: Download frostealth/yii2-presenter 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/ */
frostealth / yii2-presenter example snippets
namespace app\presenters;
use app\models\User;
use frostealth\yii2\presenter\Presenter;
/**
* Class ConcreteEntityPresenter
*
* @property User $entity
*
* @property-read string $firstName
* @property-read string $lastName
* @property-read string $fullName
* @property-read string $birthDate
*/
class UserPresenter extends Presenter
{
/**
* @return string
*/
public function getFullName()
{
return implode(' ', [$this->firstName, $this->lastName]);
}
/**
* @return string
*/
public function getBirthDate()
{
return date('y.M.d', $this->entity->birthDate);
}
/**
* @inheritdoc
* @see \yii\base\Arrayable::fields()
* @link http://www.yiiframework.com/doc-2.0/guide-rest-resources.html#fields
*/
public function fields()
{
$fields = parent::fields();
$fields[] = 'fullName';
return $fields;
}
}
namespace app\models;
use app\presenters\UserPresenter;
use frostealth\presenter\interfaces\PresentableInterface;
use frostealth\yii2\presenter\traits\PresentableTrait;
/**
* Class User
*
* @property string $firstName
* @property string $lastName
* @property string $birthDate
* @property string $passwordHash
* @property string $passwordResetToken
*
* @method UserPresenter presenter()
*/
class User extends ActiveRecord implements PresentableInterface
{
use PresentableTrait;
/**
* @inheritdoc
* @see \yii\base\Arrayable::fields()
* @link http://www.yiiframework.com/doc-2.0/guide-rest-resources.html#fields
*/
public function fields()
{
$fields = parent::fields();
unset($fields['passwordHash'], $fields['passwordResetToken']);
return $fields;
}
/**
* @return string|array
*/
protected function getPresenterClass()
{
return 'app\presenters\UserPresenter';
}
}