faustbrian/laravel-presenter-registrar

Laravel like approach to View Presenters

v1.1.0 2018-11-10 06:41 UTC

README

Build Status PHP from Packagist Latest Version License

The Problem

A lot of times nowadays we are working with dozens of third-party packages and not all of those offer an easy way of replacing models used by them. This means it can become quite a task to extend or replace those if you would like to use something like a Presenter on these. This package tries to solve this issue by registering presenters like Laravel does with Policies to allow binding any Presenter to any model so you don't need to replace third-party models.

Installation

Requirements

Require this package, with Composer, in the root directory of your project.

$ composer require faustbrian/laravel-presenter-registrar

Usage

Register presenters via PresenterServiceProvider

<?php

namespace App\Providers;

use BrianFaust\PresenterRegistrar\PresenterRegistrarServiceProvider as ServiceProvider;

class PresenterServiceProvider extends ServiceProvider
{
    protected $presenters = [
        'App\User' => 'App\Presenters\UserPresenter',
        'ThirdParty\Models\Model' => 'App\Presenters\ThirdPartyPresenter',
    ];
}

Create the previously registered presenter

<?php

namespace App\Presenters;

use BrianFaust\PresenterRegistrar\Presenter;

class UserPresenter extends Presenter
{
    public function fullName()
    {
        return "{$this->model->firstName} {$this->model->lastName}";
    }
}

Use presenter from anywhere (usually inside a view)

{{ present($user)->fullName }}

Testing

$ phpunit

Security

If you discover a security vulnerability within this package, please send an e-mail to hello@brianfaust.me. All security vulnerabilities will be promptly addressed.

Credits

License

MIT © Brian Faust