An open API service indexing awesome lists of open source software.

https://github.com/andrewdyer/view-presenters

A clean, reusable solution for separating presentation logic from models and views in PHP applications
https://github.com/andrewdyer/view-presenters

laravel php presenters view view-presenter

Last synced: 5 months ago
JSON representation

A clean, reusable solution for separating presentation logic from models and views in PHP applications

Awesome Lists containing this project

README

          

# View Presenters

A clean, reusable solution for separating presentation logic from models and views in PHP applications.

## ⚖️ License

Licensed under the [MIT license](https://opensource.org/licenses/MIT) and is free for private or commercial projects.

## ✨ Introduction

When models or views become bloated with formatting and derived data logic, **View Presenters** offer a way to offload that responsibility into dedicated presenter classes. This improves separation of concerns, testability, and reusability.

## 📦 Installation

Install via Composer:

```shell
composer require andrewdyer/view-presenters
```

## 🚀 Getting Started

### 1️⃣ Define a Presenter

Create a presenter class that extends `Presenter`:

```php
$this->user->getId(),
'forename' => $this->user->getForename(),
'surname' => $this->user->getSurname(),
];
}

public function name(): string
{
return $this->user->getForename() . ' ' . $this->user->getSurname();
}
}
```

### 2️⃣ Attach Presenter to Model

Use the `HasPresenters` trait in your model and define available presenters:

```php
UserPresenter::class,
];

public function getId(): int
{
return $this->id;
}

public function getForename(): string
{
return $this->forename;
}

public function getSurname(): string
{
return $this->surname;
}
}
```

## 📖 Usage

Access presenter attributes via the `present()` method:

```php
$user = new User();
$user->setId(1);
$user->setForename('John');
$user->setSurname('Doe');

echo $user->present()->name; // "John Doe"
```