2017 © Pedro Peláez
 

library yii2-presenter

presenter and helper console command for yii2

image

chenqd/yii2-presenter

presenter and helper console command for yii2

  • Wednesday, March 22, 2017
  • by chenqd
  • Repository
  • 1 Watchers
  • 0 Stars
  • 15 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

yii2-presenter

  参考 laracasts/presenter 实现presenter层, 调整了调用方式并新增了一个脚手架工具。, (*1)

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

使用方式

presenter例子:, (*3)

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

    }

拓展类实现:, (*4)

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

class User {

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

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

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

<h1>hello <?= $user->present()->fullName ?>!</h1>
or
<h1>hello <?= $user->present('fullName') ?>!</h1>
hello <?= $user->present('first') ?>!
hello <?= $user->present()->first ?>!

动态切换persenter

Model类处理, (*6)

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调用:, (*7)

<h1>hello <?= $user->present()->fullName ?>!</h1>
<h1>hello <?= $user->presenter()->fullName ?>!</h1>
<h1>hello <?= $user->presenter('api')->fullName ?>!</h1>
or
<h1>hello <?= $user->presenter('api', 'fullName') ?>!</h1>
hello <?= $user->presenter('api', 'first') ?>!
hello <?= $user->presenter(UserPresenter::class)->first ?>!

脚手架

进入console对应的配置文件添加, (*8)

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

如果要指定生成文件位置, (*9)

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

使用, (*10)

./yii presenter/create User

其它

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

use chenqd\presenter\PresentableTrait;

class User extend ActiveRecord
{
    use PresentableTrait;
}

TODO

  • [ ] 多model关联的实现方案
  • [ ] 由Present发起调用的实现方案

The Versions

22/03 2017

dev-master

9999999-dev

presenter and helper console command for yii2

  Sources   Download

The Requires

 

by asmodai

22/03 2017

0.3.1

0.3.1.0

presenter and helper console command for yii2

  Sources   Download

The Requires

 

by asmodai

20/03 2017

0.2.1

0.2.1.0

presenter and helper console command for yii2

  Sources   Download

The Requires

  • php >=5.5.9

 

by asmodai