2017 © Pedro Peláez
 

library laravel-auto-presenter

A system for auto-decorating models with presenter objects.

image

ryanwinchester/laravel-auto-presenter

A system for auto-decorating models with presenter objects.

  • Friday, August 5, 2016
  • by ryanwinchester
  • Repository
  • 1 Watchers
  • 1 Stars
  • 1,495 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 60 Forks
  • 0 Open issues
  • 26 Versions
  • 0 % Grown

The README.md

Laravel Auto Presenter 4

Software License Total Downloads Latest Version, (*1)

This package automatically decorates objects bound to views during the view render process., (*2)

Features

  • Automatically decorate objects bound to views
  • Automatically decorate objects within paginator instances
  • Automatically decorate objects within arrays and collections

Installing

Either PHP 5.5+ or HHVM 3.6+ are required., (*3)

To get the latest version of Laravel Auto Presenter, simply require the project using Composer:, (*4)

$ composer require ryanwinchester/laravel-auto-presenter

Instead, you may of course manually update your require block and run composer update if you so choose:, (*5)

{
    "require": {
        "ryanwinchester/laravel-auto-presenter": "^4.0"
    }
}

Then, in your config/app.php add this line to your 'providers' array., (*6)

'McCool\LaravelAutoPresenter\AutoPresenterServiceProvider',

Usage

To show how it's used, we'll pretend that we have an Eloquent Post model. It doesn't have to be Eloquent, it could be any kind of class. But, this is a normal situation. The Post model represents a blog post., (*7)

I'm using really basic code examples here, so just focus on how the auto-presenter is used and ignore the rest., (*8)

use Example\Accounts\User;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    protected $table = 'posts';
    protected $fillable = ['author_id', 'title', 'content', 'published_at'];

    public function author()
    {
        return $this->belongsTo(User::class, 'author_id');
    }
}

Also, we'll need a controller.., (*9)

use Example\Accounts\Post;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\View;

class PostsController extends Controller
{
    public function getIndex()
    {
        $posts = Post::all();
        return View::make('posts.index', compact('posts'));
    }
}

and a view..., (*10)

@foreach($posts as $post)
    <li>{{ $post->title }} - {{ $post->published_at }}</li>
@endforeach

In this example the published_at attribute is likely to be in the format: "Y-m-d H:i:s" or "2013-08-10 10:20:13". In the real world this is not what we want in our view. So, let's make a presenter that lets us change how the data from the Post class is rendered within the view., (*11)

use Carbon\Carbon;
use Example\Accounts\Post;
use McCool\LaravelAutoPresenter\BasePresenter;

class PostPresenter extends BasePresenter
{
    public function __construct(Post $resource)
    {
        $this->wrappedObject = $resource;
    }

    public function published_at()
    {
        $published = $this->wrappedObject->published_at;

        return Carbon::createFromFormat('Y-m-d H:i:s', $published)
            ->toFormattedDateString();
    }
}

Here, the automatic presenter decorator is injecting the Post model that is to be decorated. Please be aware that the constructor parameter should always be named $resource to allow Laravel's IoC container to correctly resolve the dependency., (*12)

We need the post class to implement the interface., (*13)

use Example\Accounts\User;
use Example\Blog\PostPresenter;
use McCool\LaravelAutoPresenter\HasPresenter;
use Illuminate\Database\Eloquent\Model;

class Post extends Model implements HasPresenter
{
    protected $table = 'posts';
    protected $fillable = ['author_id', 'title', 'content', 'published_at'];

    public function author()
    {
        return $this->belongsTo(User::class, 'author_id');
    }

    public function getPresenterClass()
    {
        return PostPresenter::class;
    }
}

Now, with no additional changes our view will show the date in the desired format., (*14)

Troubleshooting

If an object isn't being decorated correctly in the view then there's a good chance that it's simply not in existence when the view begins to render. For example, lazily-loaded relationships won't be decorated. You can fix this by eager-loading them instead. Auth::user() will never be decorated. I prefer to bind $currentUser to my views, anyway., (*15)

Security

If you discover a security vulnerability within this package, please send an e-mail to Graham Campbell at graham@alt-three.com. All security vulnerabilities will be promptly addressed., (*16)

License

Laravel Auto Presenter is licensed under The MIT License (MIT)., (*17)

The Versions

04/10 2015

4.1.0

4.1.0.0

A system for auto-decorating models with presenter objects.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Graham Campbell

laravel eloquent lpm presenter

26/06 2015

4.0.0

4.0.0.0

A system for auto-decorating models with presenter objects.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Graham Campbell

laravel eloquent lpm presenter

26/06 2015

3.1.x-dev

3.1.9999999.9999999-dev

A system for auto-decorating models with presenter objects.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Graham Campbell

laravel eloquent lpm presenter

26/06 2015

3.1.1

3.1.1.0

A system for auto-decorating models with presenter objects.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Graham Campbell

laravel eloquent lpm presenter

29/05 2015

3.1.0

3.1.0.0

A system for auto-decorating models with presenter objects.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Graham Campbell

laravel eloquent lpm presenter

18/05 2015

3.0.1

3.0.1.0

A system for auto-decorating models with presenter objects.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Graham Campbell

laravel eloquent lpm presenter

04/02 2015

3.0.0

3.0.0.0

A system for auto-decorating models with presenter objects.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Graham Campbell

laravel eloquent lpm presenter

07/01 2015

2.2.2

2.2.2.0

A system for auto-decorating models with presenter objects.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Graham Campbell

laravel eloquent lpm presenter

01/01 2015

3.0.0-beta8

3.0.0.0-beta8

A system for auto-decorating models with presenter objects.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Graham Campbell

laravel eloquent lpm presenter

28/10 2014

3.0.0-beta7

3.0.0.0-beta7

A system for auto-decorating models with presenter objects.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Graham Campbell

laravel eloquent lpm presenter

27/10 2014

3.0.0-beta6

3.0.0.0-beta6

A system for auto-decorating models with presenter objects.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Graham Campbell

laravel eloquent lpm presenter

27/10 2014

3.0.0-beta5

3.0.0.0-beta5

A system for auto-decorating models with presenter objects.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Graham Campbell

laravel eloquent lpm presenter

25/10 2014

3.0.0-beta4

3.0.0.0-beta4

A system for auto-decorating models with presenter objects.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Graham Campbell

laravel eloquent lpm presenter

25/10 2014

3.0.0-beta3

3.0.0.0-beta3

A system for auto-decorating models with presenter objects.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Graham Campbell

laravel eloquent lpm presenter

26/08 2014

2.2.1

2.2.1.0

A system for auto-decorating models with presenter objects.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Graham Campbell

laravel eloquent lpm presenter

25/08 2014

2.2.0

2.2.0.0

A system for auto-decorating models with presenter objects.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Graham Campbell

laravel eloquent lpm presenter

25/08 2014

1.2.0

1.2.0.0

A system for auto-decorating models with presenter objects.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Graham Campbell

laravel eloquent lpm presenter

06/06 2014

2.1.0

2.1.0.0

A system for auto-decorating models with presenter objects.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent lpm presenter

06/06 2014

2.0.0

2.0.0.0

A system for auto-decorating models with presenter objects.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent lpm presenter

05/06 2014

1.1.0

1.1.0.0

A system for auto-decorating models with presenter objects.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent lpm presenter

10/09 2013

1.0.0

1.0.0.0

A system for auto-decorating models with presenter objects.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent lpm presenter