vnphp/presenter-bundle

Symfony2 Presenter pattern support.

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

This package is auto-updated.

Last update: 2024-10-14 11:44:13 UTC


README

build status code quality

Installation

composer require vnphp/presenter-bundle

You need to have git installed.

Configure

1. Add the bundle to your AppKernel.php:

<?php 

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

2. Update your model

<?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. Create presenter class

<?php

use Vnphp\PresenterBundle\Presenter\Presenter;

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

4. Add presenter service.

It is required to set scope: prototype to the definition.

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

Usage

Controller

<?php

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

Twig

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