rickselby / laravel-auto-presenter-mapper
Define presenters in a service provider rather than in the model
Installs: 5 284
Dependents: 0
Suggesters: 0
Security: 0
Stars: 4
Watchers: 2
Forks: 1
Open Issues: 0
Requires
Requires (Dev)
- graham-campbell/testbench: ^5.0
- phpunit/phpunit: ^6.0|^7.0
README
This is an extension to the excellent laravel-auto-presenter that allows you to define presenters in a service provider or on-the-fly, rather than directly on the model.
Compatibility Chart
Installing
Require the project using Composer:
$ composer require rickselby/laravel-auto-presenter-mapper
Laravel 5.5+ will auto-discover the package.
For Laravel 5.1-5.4, you should only add this service provider, not the original laravel-auto-presenter
service provider, as this one
extends it.
In your config/app.php
add this line to your 'providers' array...
RickSelby\LaravelAutoPresenterMapper\AutoPresenterMapperServiceProvider::class,
... and this line to your 'facades' array.
'Presenters' => \RickSelby\LaravelAutoPresenterMapper\Facades\AutoPresenterMapperFacade::class,
Usage
Read the docs at github.com/laravel-auto-presenter/laravel-auto-presenter for the basic use cases.
With this package, instead of altering your models to implement HasPresenter
, you can define the presenters in a service
provider using the facade. For example:
public function register() { \Presenters::map(User::class, UserPresenter::class); }
This will work exactly how the laravel-auto-presenter works; any User
models passed to a view will be wrapped in UserPresenter
.
The map
function also takes an array:
public function register() { \Presenters::map([ User::class => UserPresenter::class, ... ]); }
If you wish to declare a mapping on-the-fly, or override a mapping in a specific instance, the facade can be called from anywhere:
public function show(User $user) { \Presenters::map(User::class, UserJSONPresenter::class); ... }
Decoratable
To mimic the Decoratable
interface of the parent package, you can call decoratable
:
public function register() { \Presenters::decorate(User::class); \Presenters::decorate([ User::class, ... ]); ... }
License
Laravel Auto Presenter Mapper is licensed under The MIT License (MIT).