2017 © Pedro Peláez
 

library presenter

Simple view presenters

image

laracasts/presenter

Simple view presenters

  • Saturday, September 13, 2014
  • by laracasts
  • Repository
  • 24 Watchers
  • 641 Stars
  • 582,759 Installations
  • PHP
  • 59 Dependents
  • 1 Suggesters
  • 80 Forks
  • 22 Open issues
  • 8 Versions
  • 7 % Grown

The README.md

Easy View Presenters

So you have those scenarios where a bit of logic needs to be performed before some data (likely from your entity) is displayed from the view., (*1)

  • Should that logic be hard-coded into the view? No.
  • Should we instead store the logic in the model? No again!

Instead, leverage view presenters. That's what they're for! This package provides one such implementation., (*2)

Install

Pull this package in through Composer., (*3)

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

Usage

The first step is to store your presenters somewhere - anywhere. These will be simple objects that do nothing more than format data, as required., (*4)

Here's an example of a presenter., (*5)

use Laracasts\Presenter\Presenter;

class UserPresenter extends Presenter {

    public function fullName()
    {
        return $this->first . ' ' . $this->last;
    }

    public function accountAge()
    {
        return $this->created_at->diffForHumans();
    }

}

Next, on your entity, pull in the Laracasts\Presenter\PresentableTrait trait, which will automatically instantiate your presenter class., (*6)

Here's an example - maybe a Laravel User model., (*7)

<?php

use Laracasts\Presenter\PresentableTrait;

class User extends \Eloquent {

    use PresentableTrait;

    protected $presenter = 'UserPresenter';

}

That's it! You're done. Now, within your view, you can do:, (*8)

    <h1>Hello, {{ $user->present()->fullName }}</h1>

Notice how the call to the present() method (which will return your new or cached presenter object) also provides the benefit of making it perfectly clear where you must go, should you need to modify how a full name is displayed on the page., (*9)

Have fun!, (*10)

Jeffrey @ https://laracasts.com, (*11)

The Versions

13/09 2014

dev-master

9999999-dev

Simple view presenters

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel view presenter

13/09 2014

0.2.0

0.2.0.0

Simple view presenters

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel view presenter

13/09 2014

0.2.1

0.2.1.0

Simple view presenters

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel view presenter

01/04 2014

0.1.4

0.1.4.0

Simple view presenters

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel view presenter

24/03 2014

0.1.3

0.1.3.0

Simple view presenters

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel view presenter

24/03 2014

0.1.2

0.1.2.0

Simple view presenters

  Sources   Download

The Requires

 

The Development Requires

laravel view presenter

24/03 2014

0.1.1

0.1.1.0

Simple view presenters

  Sources   Download

The Requires

 

The Development Requires

laravel view presenter

24/03 2014

0.1.0

0.1.0.0

Simple view presenters

  Sources   Download

The Requires

 

The Development Requires

laravel view presenter