PHPackages                             frostealth/yii2-presenter - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. frostealth/yii2-presenter

ActiveYii2-extension[Templating &amp; Views](/categories/templating)

frostealth/yii2-presenter
=========================

Yii2 View Presenter

v0.2.0(10y ago)1310.4k2MITPHPPHP &gt;=5.4

Since Dec 12Pushed 9y ago2 watchersCompare

[ Source](https://github.com/frostealth/yii2-presenter)[ Packagist](https://packagist.org/packages/frostealth/yii2-presenter)[ RSS](/packages/frostealth-yii2-presenter/feed)WikiDiscussions master Synced today

READMEChangelog (3)Dependencies (2)Versions (4)Used By (0)

Yii2 View Presenters
====================

[](#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.

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

Installation
------------

[](#installation)

Run the [Composer](http://getcomposer.org/download/) command to install the latest stable version:

```
composer require frostealth/yii2-presenter @stable
```

Usage
-----

[](#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.

Here's an example of a presenter.

```
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.

Here's an example of an presentable model.

```
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:

```

    Name

    Birth Date

```

### Yii2 REST

[](#yii2-rest)

Here's an example of an controller.

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

[](#license)

The MIT License (MIT). See [LICENSE.md](https://github.com/frostealth/yii2-presenter/blob/master/LICENSE.md) for more information.

###  Health Score

30

—

LowBetter than 62% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity28

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~1 days

Total

3

Last Release

3853d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/3209d007425f192de0e0941e6181d2dfd36ed4f00714675ca7fb525c6fd27ed2?d=identicon)[frostealth](/maintainers/frostealth)

---

Top Contributors

[![frostealth](https://avatars.githubusercontent.com/u/1785217?v=4)](https://github.com/frostealth "frostealth (6 commits)")

---

Tags

entitypresenterviewyiiyii2yii2-extensionyii2presenter

### Embed Badge

![Health badge](/badges/frostealth-yii2-presenter/health.svg)

```
[![Health](https://phpackages.com/badges/frostealth-yii2-presenter/health.svg)](https://phpackages.com/packages/frostealth-yii2-presenter)
```

###  Alternatives

[craftcms/cms

Craft CMS

3.6k3.6M3.1k](/packages/craftcms-cms)[bubasuma/yii2-simplechat

A simple chat for your yii2 application

889.6k](/packages/bubasuma-yii2-simplechat)[zxbodya/yii2-tinymce

Yii2 extension to use TinyMce with Compressor and FileManager

1732.9k3](/packages/zxbodya-yii2-tinymce)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
