2017 © Pedro Peláez
 

library presenter

Simple view presenter library for Laravel.

image

lewis/presenter

Simple view presenter library for Laravel.

  • Thursday, September 8, 2016
  • by jasonlewis
  • Repository
  • 1 Watchers
  • 17 Stars
  • 3,995 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 6 Forks
  • 7 Open issues
  • 11 Versions
  • 2 % Grown

The README.md

A view presenter is an incredibly useful way of decorating objects bound to your views. This allows you to perform view related logic in a sensible place instead of placing it directly in your views, or worse, your models., (*1)

There are several other libraries out their that provide similar functionality. This package is merely my preferred implementation as everything is configured in isolation to the object being decorated., (*2)

Install

You can install this package using Composer:, (*3)

$ composer require lewis/presenter

Or in your composer.json:, (*4)

{
    "require": {
        "lewis/presenter": "0.1.*"
    }
}

Once you've run composer update you'll need to register the service provider in your config/app.php file., (*5)

'providers' => [
    Lewis\Presenter\PresenterServiceProvider::class
]

Usage

Configuring Presenters

There's several ways to configure your presenters. First, you can utilize the configuration file, which can be published using the following command:, (*6)

$ php artisan vendor:publish --provider="Lewis\Presenter\PresenterServiceProvider"

There are no presenters configured by default. The published file merely contains an explanation on how to configure your presenters. You must provide an array of key/value pairs linking your object to its presenter., (*7)

return [

    App\User::class => App\Presenters\UserPresenter::class,
    App\Post::class => App\Presenters\PostPresenter::class

];

If you'd prefer you can set an array of presenters directly on the decorator. You might choose do to this from a service provider., (*8)

$this->app['decorator']->setBindings([
    \App\User::class => \App\Presenters\UserPresenter::class,
    \App\Post::class => \App\Presenters\PostPresenter::class
]);

Lastly, you can configure presenters one at a time using the register method, again, from within a provider., (*9)

$this->app['decorator']->register(\App\User::class, \App\Presenters\UserPresenter::class);

Creating Presenters

A presenter should extend from Lewis\Presenter\AbstractPresenter, however, it is NOT required, but highly recommended, as you'll have access to several methods and magic methods that provide some useful functionality., (*10)

I like to keep my presenters within a Presenters folder, however, you may organize things in whichever way you prefer., (*11)

namespace App\Presenters;

use Lewis\Presenter\AbstractPresenter;

class UserPresenter extends AbstractPresenter
{

}

If you wish to inject dependencies the only requirements are that you name a parameter $object so that Laravel can correctly inject the bound object and that you call the parent constructor., (*12)

namespace App\Presenters;

use App\SomeNamespace\SomeClass;
use Lewis\Presenter\AbstractPresenter;

class UserPresenter extends AbstractPresenter
{
    protected $class;

    public function __construct($object, SomeClass $class)
    {
        $this->class = $class;

        parent::__construct($object);
    }
}

Your presenter can then define methods to perform logic that can be used in your views., (*13)

public function prettySlug()
{
    return '/'.ltrim($this->slug, '/');
}

You can reference properties on the wrapped object directly (as above), or by using the $object property., (*14)

public function prettySlug()
{
    return '/'.ltrim($this->object->slug, '/');
}

From Within Views

Now that you've configured your presenters and created them, you just need to use them from within your views. It's a simple matter of calling the method or property as you define it., (*15)

{{ $post->prettySlug }}

Or:

{{ $post->prettySlug() }}

You can also still access you relations and other model attributes., (*16)

{{ $post->title }}

@foreach($post->comments as $comment)
    ...
@endforeach

Enjoy

That's about all there is to it., (*17)

The Versions

08/09 2016

dev-master

9999999-dev

Simple view presenter library for Laravel.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel decorator view presenter

19/02 2016

v1.0.1

1.0.1.0

Simple view presenter library for Laravel.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel decorator view presenter

27/09 2015

v1.0.0

1.0.0.0

Simple view presenter library for Laravel.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel decorator view presenter

23/09 2015

v0.1.7

0.1.7.0

Simple view presenter library for Laravel.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel decorator view presenter

13/09 2015

v0.1.6

0.1.6.0

Simple view presenter library for Laravel.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel decorator view presenter

11/09 2015

v0.1.5

0.1.5.0

Simple view presenter library for Laravel.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel decorator view presenter

11/09 2015

v0.1.4

0.1.4.0

Simple view presenter library for Laravel.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel decorator view presenter

09/09 2015

v0.1.3

0.1.3.0

Simple view presenter library for Laravel.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel decorator view presenter

08/09 2015

v0.1.2

0.1.2.0

Simple view presenter library for Laravel.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel decorator view presenter

08/09 2015

v0.1.1

0.1.1.0

Simple view presenter library for Laravel.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel decorator view presenter

07/09 2015

v0.1.0

0.1.0.0

Simple view presenter library for Laravel.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel decorator view presenter