All Projects → frostealth → yii2-presenter

frostealth / yii2-presenter

Licence: MIT license
Yii2 View Presenter

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to yii2-presenter

ar-role
ActiveRecord behavior, which provides relation roles (table inheritance)
Stars: ✭ 34 (+161.54%)
Mutual labels:  yii2, yii, yii2-extension
Balance
Balance accounting (bookkeeping) system based on debit and credit principle
Stars: ✭ 162 (+1146.15%)
Mutual labels:  yii2, yii, yii2-extension
Ar Position
ActiveRecord behavior, which provides ability for custom records order setup
Stars: ✭ 107 (+723.08%)
Mutual labels:  yii2, yii, yii2-extension
Ar Linkmany
ActiveRecord behavior for saving many-to-many relations
Stars: ✭ 83 (+538.46%)
Mutual labels:  yii2, yii, yii2-extension
content
Content management system for Yii2
Stars: ✭ 54 (+315.38%)
Mutual labels:  yii2, yii, yii2-extension
Yii2 Aws S3
An Amazon S3 component for Yii2
Stars: ✭ 86 (+561.54%)
Mutual labels:  yii2, yii, yii2-extension
Yii2 Assets Auto Compress
Automatic compilation of js + css + html
Stars: ✭ 147 (+1030.77%)
Mutual labels:  yii2, yii, yii2-extension
Config
Yii2 application runtime configuration support
Stars: ✭ 54 (+315.38%)
Mutual labels:  yii2, yii, yii2-extension
install
basic script for project installation
Stars: ✭ 17 (+30.77%)
Mutual labels:  yii2, yii, yii2-extension
Ar Softdelete
Soft delete behavior for ActiveRecord
Stars: ✭ 188 (+1346.15%)
Mutual labels:  yii2, yii, yii2-extension
Csv Grid
Yii2 extension for CSV export
Stars: ✭ 83 (+538.46%)
Mutual labels:  yii2, yii, yii2-extension
behavior-trait
Allows handling events via inline declared methods, which can be added by traits
Stars: ✭ 18 (+38.46%)
Mutual labels:  yii2, yii, yii2-extension
Yii2 Schemadump
Generate the schema from an existing database.
Stars: ✭ 78 (+500%)
Mutual labels:  yii2, yii, yii2-extension
Admin
Admin pack (actions, widgets, etc) for Yii2
Stars: ✭ 100 (+669.23%)
Mutual labels:  yii2, yii, yii2-extension
Sitemap
Site map creation support
Stars: ✭ 59 (+353.85%)
Mutual labels:  yii2, yii, yii2-extension
File Storage
File storage abstraction for Yii2
Stars: ✭ 116 (+792.31%)
Mutual labels:  yii2, yii, yii2-extension
filedb
ActiveRecord for static data definitions based on files
Stars: ✭ 72 (+453.85%)
Mutual labels:  yii2, yii, yii2-extension
yii2-facades
Facades for Yii 2
Stars: ✭ 21 (+61.54%)
Mutual labels:  yii2, yii, yii2-extension
Crontab
Yii2 extension for crontab support
Stars: ✭ 170 (+1207.69%)
Mutual labels:  yii2, yii, yii2-extension
ar-search
Provides unified search model for Yii ActiveRecord
Stars: ✭ 31 (+138.46%)
Mutual labels:  yii2, yii, yii2-extension

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

Run the Composer command to install the latest stable version:

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.

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:

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

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].