霜之暗号/yii2-presenter

Yii2 视图表示器

安装数: 10,217

依赖: 0

建议者: 0

安全: 0

星星: 13

关注者: 3

分支: 2

开放问题: 0

类型:yii2-extension

v0.2.0 2015-12-15 06:25 UTC

This package is not auto-updated.

Last update: 2024-09-14 18:36:46 UTC


README

在您需要执行一些逻辑(可能来自您的实体)后才能从视图显示数据的情况下,您有这些场景。

  • 该逻辑是否应该硬编码到视图中?不。
  • 我们是否应该将逻辑存储在模型中?再不。

相反,利用视图表示器。这正是它们的作用!本软件包提供了一种实现。

安装

运行 Composer 命令以安装最新稳定版本

composer require frostealth/yii2-presenter @stable

用法

第一步是将您的表示器存储在某个地方 - 任何地方。这些将是简单的对象,除了格式化所需的数据外,不做任何事情。

以下是一个表示器的示例。

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 https://yiiframework.cn/doc-2.0/guide-rest-resources.html#fields
     */
    public function fields()
    {
        $fields = parent::fields();
        $fields[] = 'fullName';
        
        return $fields;
    }
}

接下来,在您的实体中引入 frostealth\yii2\presenter\traits\PresentableTrait 特性,这将自动实例化您的表示器类。

以下是一个可表示的模型示例。

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 https://yiiframework.cn/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';
    }
}

现在,在您的视图中,您可以这样做

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

Yii2 REST

以下是一个控制器示例。

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

许可证

MIT 许可证(MIT)。有关更多信息,请参阅 LICENSE.md