Giter Site home page Giter Site logo

shibahuasheng / laravel-auto-presenter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from laravel-auto-presenter/laravel-auto-presenter

0.0 2.0 0.0 252 KB

A system for auto-decorating models with presenter objects.

License: MIT License

PHP 100.00%

laravel-auto-presenter's Introduction

Laravel Auto Presenter 4

StyleCI Status Build Status Software License Total Downloads Latest Version

This package automatically decorates objects bound to views during the view render process.

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.

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

$ composer require mccool/laravel-auto-presenter

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

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

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

'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.

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

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..

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...

@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.

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.

We need the post class to implement the interface.

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.

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.

Security

If you discover a security vulnerability within this package, please send an e-mail to Graham Campbell at [email protected]. All security vulnerabilities will be promptly addressed.

License

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

laravel-auto-presenter's People

Contributors

grahamcampbell avatar kirkbushell avatar rquadling avatar rtablada avatar mrsimonbennett avatar hardevine avatar jamierumbelow avatar bonroyage avatar stidges avatar hendore avatar nyholm avatar bl4ck-bird avatar webcraft avatar

Watchers

James Cloos avatar iveskim avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.