A clean, reusable solution for separating presentation logic from models and views in PHP applications.
Licensed under the MIT license and is free for private or commercial projects.
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.
Install via Composer:
composer require andrewdyer/view-presenters
Create a presenter class that extends Presenter
:
<?php
namespace App\Presenters;
use App\Models\User;
use Anddye\ViewPresenters\Presenter;
class UserPresenter extends Presenter
{
public function __construct(readonly private User $user) {}
public function defaultAttributes(): array
{
return [
'id' => $this->user->getId(),
'forename' => $this->user->getForename(),
'surname' => $this->user->getSurname(),
];
}
public function name(): string
{
return $this->user->getForename() . ' ' . $this->user->getSurname();
}
}
Use the HasPresenters
trait in your model and define available presenters:
<?php
namespace App\Models;
use App\Presenters\UserPresenter;
use Anddye\ViewPresenters\HasPresenters;
class User {
use HasPresenters;
protected int $id;
protected string $forename;
protected string $surname;
protected array $presenters = [
'default' => UserPresenter::class,
];
public function getId(): int
{
return $this->id;
}
public function getForename(): string
{
return $this->forename;
}
public function getSurname(): string
{
return $this->surname;
}
}
Access presenter attributes via the present()
method:
$user = new User();
$user->setId(1);
$user->setForename('John');
$user->setSurname('Doe');
echo $user->present()->name; // "John Doe"