2017 © Pedro Peláez
 

yii2-extension yii2-presenter

Yii2 View Presenter

image

frostealth/yii2-presenter

Yii2 View Presenter

  • Thursday, February 9, 2017
  • by frostealth
  • Repository
  • 2 Watchers
  • 10 Stars
  • 1,744 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 4 Versions
  • 30 % Grown

The README.md

Yii2 View Presenters

So you have those scenarios where a bit of logic needs to be performed before some data (likely from your entity) is displayed from the view., (*1)

  • Should that logic be hard-coded into the view? No.
  • Should we instead store the logic in the model? No again!

Instead, leverage view presenters. That's what they're for! This package provides one such implementation., (*2)

Installation

Run the Composer command to install the latest stable version:, (*3)

composer require frostealth/yii2-presenter @stable

Usage

The first step is to store your presenters somewhere - anywhere. These will be simple objects that do nothing more than format data, as required., (*4)

Here's an example of a presenter., (*5)

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

Next, on your entity, pull in the frostealth\yii2\presenter\traits\PresentableTrait trait, which will automatically instantiate your presenter class., (*6)

Here's an example of an presentable model., (*7)

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

Now, within your view, you can do:, (*8)

<dl>
    <dt>Name</dt>
    <dd><?= $model->presenter()->fullName ?></dd>

    <dt>Birth Date</dt>
    <dd><?= $model->presenter()->birthDate ?></dd>
</dl>

Yii2 REST

Here's an example of an controller., (*9)

namespace app\controllers;

use yii\rest\ActiveController;

class UserController extends ActiveController
{
    /** @inheritdoc */
    public $serializer = 'frostealth\yii2\presenter\rest\Serializer';

    /** @inheritdoc */
    public $className = 'app\models\User';
}

License

The MIT License (MIT). See LICENSE.md for more information., (*10)

The Versions

09/02 2017

dev-master

9999999-dev

Yii2 View Presenter

  Sources   Download

MIT

The Requires

 

yii2 presenter

15/12 2015

v0.2.0

0.2.0.0

Yii2 View Presenter

  Sources   Download

MIT

The Requires

 

yii2 presenter

14/12 2015

v0.1.1

0.1.1.0

Yii2 View Presenter

  Sources   Download

MIT

The Requires

 

yii2 presenter

12/12 2015

v0.1.0

0.1.0.0

Yii2 View Presenter

  Sources   Download

MIT

The Requires

 

yii2 presenter