PHPackages                             chenqd/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. [CLI &amp; Console](/categories/cli)
4. /
5. chenqd/yii2-presenter

ActiveLibrary[CLI &amp; Console](/categories/cli)

chenqd/yii2-presenter
=====================

presenter and helper console command for yii2

0.3.1(9y ago)025PHPPHP &gt;=5.5.9

Since Mar 20Pushed 9y ago1 watchersCompare

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

READMEChangelogDependencies (1)Versions (3)Used By (0)

yii2-presenter
==============

[](#yii2-presenter)

 参考 [laracasts/presenter](https://github.com/laracasts/Presenter) 实现presenter层, 调整了调用方式并新增了一个脚手架工具。

个人理解Presenter是提供给view针对model的一种横向扩展方式, 就实现来说是对behavior的简化版。

使用方式
----

[](#使用方式)

presenter例子:

```
    use chenqd\presenter\BasePresenter;

    /**
    * @mixin \app\models\User
    * @property \app\models\User $entity
    */
    class UserPresenter extends BasePresenter {

        public function fullName()
        {
            return $this->first . ' ' . $this->last;
        }

        public function first()
        {
            return ucfirst($this->entity->first);
        }

    }

```

拓展类实现:

```
//不一定局限于model
use chenqd\presenter\PresentableTrait;

class User {

    use PresentableTrait;
    protected $presenter = UserPresenter::class;

    public $first = 'php';
    public $last = 'world';
}

```

view调用: (实现很简单可以简单翻阅下源码)

```
hello ！
or
hello ！
hello ！
hello ！

```

动态切换persenter
-------------

[](#动态切换persenter)

Model类处理

```
use chenqd\presenter\PresentableTrait;

class User {

    use PresentableTrait;
    public function presenterMap()
    {
        return [
            'default' => UserPresenter::class,
            'api' => UserPresenter::class,
        ];
    }

    public $first = 'php';
    public $last = 'world';
}
```

view调用:

```
hello ！
hello ！
hello ！
or
hello ！
hello ！
hello ！

```

脚手架
---

[](#脚手架)

进入console对应的配置文件添加

```
//advance模板: common/config/main.php
//basic模板: config/console.php
return [
     //...其它配置
    'controllerMap' => [
        'presenter'=>\chenqd\presenter\PresenterController::class,
    ],
    //...其它配置
];
```

如果要指定生成文件位置

```
'controllerMap' => [
    'presenter' => [
        'class' => \chenqd\presenter\PresenterController::class,
        'base_path' => '@common/models',
        'namespace' => 'common\models',
    ],
],
```

使用

```
./yii presenter/create User

```

其它
--

[](#其它)

默认脚手架生成presenter到models/presenters文件下 (高级模板在common/models/presenters), 在model中使用时可以不指定 $presenter

```
use chenqd\presenter\PresentableTrait;

class User extend ActiveRecord
{
    use PresentableTrait;
}

```

TODO
----

[](#todo)

- 多model关联的实现方案
- 由Present发起调用的实现方案

###  Health Score

23

—

LowBetter than 26% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity49

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 ~2 days

Total

2

Last Release

3386d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/6767197?v=4)[chenqd](/maintainers/chenqd)[@chenqd](https://github.com/chenqd)

---

Top Contributors

[![asmodai1985](https://avatars.githubusercontent.com/u/6767187?v=4)](https://github.com/asmodai1985 "asmodai1985 (14 commits)")

---

Tags

presenteryii2

### Embed Badge

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

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

###  Alternatives

[seld/cli-prompt

Allows you to prompt for user input on the command line, and optionally hide the characters they type

24726.4M22](/packages/seld-cli-prompt)[illuminate/console

The Illuminate Console package.

13045.3M6.2k](/packages/illuminate-console)[styleci/cli

The CLI tool for StyleCI

71464.1k9](/packages/styleci-cli)[winbox/args

Windows command-line formatter

20718.9k21](/packages/winbox-args)

PHPackages © 2026

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