Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/laracasts/Presenter
Easy view presenters in your apps.
https://github.com/laracasts/Presenter
Last synced: 3 months ago
JSON representation
Easy view presenters in your apps.
- Host: GitHub
- URL: https://github.com/laracasts/Presenter
- Owner: laracasts
- License: mit
- Created: 2014-03-24T00:12:13.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2024-05-10T00:36:12.000Z (9 months ago)
- Last Synced: 2024-09-08T13:44:58.347Z (5 months ago)
- Language: PHP
- Homepage: https://packagist.org/packages/laracasts/presenter
- Size: 17.6 KB
- Stars: 864
- Watchers: 28
- Forks: 111
- Open Issues: 25
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
- laravel-awesome - Presenter - Presenter for Models (Popular Packages)
README
# 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.
- 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.
## Install
Pull this package in through Composer.
```js
{
"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.
Here's an example of a presenter.
```php
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.
Here's an example - maybe a Laravel `User` model.
```php
Hello, {{ $user->present()->fullName }}
```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.
Have fun!
Jeffrey @ [https://laracasts.com](https://laracasts.com)