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
- Host: GitHub
- URL: https://github.com/andrewdyer/view-presenters
- Owner: andrewdyer
- Created: 2020-06-05T15:39:59.000Z (about 6 years ago)
- Default Branch: main
- Last Pushed: 2025-05-27T16:33:38.000Z (about 1 year ago)
- Last Synced: 2025-07-02T11:19:29.269Z (about 1 year ago)
- Topics: laravel, php, presenters, view, view-presenter
- Language: PHP
- Homepage:
- Size: 18.6 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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"
```