vnphp/presenter-bundle

支持 Symfony2 Presenter 模式。

v0.2 2016-11-18 11:38 UTC

This package is auto-updated.

Last update: 2024-09-14 11:28:16 UTC


README

build status code quality

安装

composer require vnphp/presenter-bundle

您需要安装 git

配置

1. 将包添加到您的 AppKernel.php

<?php 

$bundles = array(          
            new \Vnphp\PresenterBundle\VnphpPresenterBundle(),
        );

2. 更新您的模型

<?php

use Vnphp\PresenterBundle\Presenter\PresentableInterface;
use Vnphp\PresenterBundle\Presenter\PresentableTrait;

class User implements PresentableInterface
{
    use PresentableTrait;
    
    /**
     * @return string service name for presenter
     */
    public function getPresenterService()
    {
        return 'presenter.user';
    }
}

3. 创建展示类

<?php

use Vnphp\PresenterBundle\Presenter\Presenter;

class UserPresenter extends Presenter
{
    public function getFullName()
    {
        return "Full Name";
    }
}

4. 添加展示服务。

需要在定义中设置 scope: prototype

services:
    presenter.user:
        class: 'UserPresenter'
        scope: 'prototype'

使用方法

控制器

<?php

$userPresenter = $this->container->get('vnphp_presenter.factory')
    ->getPresenter($user);
$userPresenter->getFullName();

Twig

{% set user_presenter = presenter(user) %}
{{ user_presenter.fullName }}