Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
P

presenter

> 后端框架
Open source

Decorate your objects using presenters. Primarily to keep presentation logic out of your models.

345 stars0 likes0 views
WebsiteGitHub

About

Decorate your objects using presenters. Primarily to keep presentation logic out of your models.

Presenter

Decorate your objects using presenters. Primarily to keep presentation logic out of your models.

Ping me @robboclancy for any urgent issues, github isn't always correctly notifying me.

This library provides a simple class to help make a Presenter for your objects or arrays. It also has little extras for use within Laravel with minimal extra code in your controllers (in most cases no extra code).

Table of Contents

  • Installation
    • Composer
    • Manually
    • Laravel
  • Usage
    • General Usage
    • Manually Initiate
    • Laravel Usage
    • Array Usage
    • Extending the Decorator
  • Change Log

Installation

bash
composer require robclancy/presenter:^2.0

or

Add robclancy/presenter to the "require" section of your composer.json file.

json
"robclancy/presenter": "^2.0"

Run composer update to get the latest version of the package.

Laravel

This package comes with an optional service provider for Laravel 5.8+ id.'-'.$this->username; } }

…

php

class UserPresenter extends Robbo\Presenter\Presenter {

public function presentUrl()
{
	return $this->id.'-'.$this->username;
}

}


Now the presenter will call this new method when you execute `$user->url`. Further more you can access this method via `ArrayAccess` by calling `$user['url']`. More on `ArrayAccess` support below.

### Manually Initiate

As mentioned in the above section to create a presenter you simply initiate with the `new` keyword and inject your object or array.

```php

class User {
	// ...
}

class UserPresenter extends Robbo\Presenter\Presenter {

	// ...
}

$user = new User;

// handle stuff here

// make sure to "convert" to a presenter before the object gets to your views
$user = new UserPresenter($user);

// Can also create a presenter for arrays
$user = [
	'id' => 1,
	'username' => 'Robbo',
];

// same as before
$user = new UserPresenter($user);

…

php

class UserPresenter extends Robbo\Presenter\Presenter {

	// ...
}

class User implements Robbo\Presenter\PresentableInterface {

	/**
	 * Return a created presenter.
	 *
	 * @return Robbo\Presenter\Presenter
	 */
	public function getPresenter()
	{
		return new UserPresenter($this);
	}
}

…

php

$user = [
	'id' => 1,
	'username' => 'Robbo',
];

class UserPresenter extends Robbo\Presenter\Presenter {

	public function presentUrl()
	{
		// This will work exactly the same as previous examples
		return $this->id.'-'.$this->username;

		// You can also do this...
		return $this['id'].'-'.$this['username'];
	}
}

// Now we create a presenter much the same as before
$user = new UserPresenter($user);

// In our views we can use the $user as if it were still an array
echo 'Hello, ', $user['username'];

// Or even treat it like the object that it is
echo 'Hello, ', $user->username;

// And like other examples, we can present the url in the same way
echo 'The URL: ', $user->url;
echo 'And again: ', $user['url'];

…

php

use Robbo\Presenter\Decorator as BaseDecorator;

class Decorator extends BaseDecorator {

	/*
     * If this variable implements Robbo\Presenter\PresentableInterface then turn it into a presenter.
     *
     * @param  mixed $value
     * @return mixed $value
    */
    public function decorate($value)
    {
    	if (is_object($value) and isset($value->presenter))
    	{
    		$presenter = $value->presenter;
    		return new $presenter;
    	}

    	return parent::decorate($value);
    }
}

To use your new decorator either add the following to start/global.php or into your own service provider.

php

// In start/global.php

App::make('presenter.decorator', App::share(function($app)
{
	$decorator = new Decorator;

	Robbo\Presenter\Presenter::setExtendedDecorator($decorator);
	return $decorator;
}));

// In a service provider's 'register' method

$this->app['presenter.decorator'] = $this->app->share(function($app)
{
	$decorator = new Decorator;

	Robbo\Presenter\Presenter::setExtendedDecorator($decorator);
	return $decorator;
});

And that is all there is to it. You can easily automate the creation of presenters to suit your workflow using this method.

Change Log

3.0

  • minimum PHP version is now 8.1
  • fixed PHP 8.1 ArrayAccess deprecations

2.0.1

  • branched off 2.x to support < PHP 8.1
  • modernized the repository somewhat
  • updated documentation

2.0.0

  • minimum Laravel version is now 5.8
  • minimum PHP version is now 7.1
  • improved the view factory integration which should be future proof

1.4.0

  • added Laravel package discovery, thanks luisdalmolin
  • use the contract for the Laravel view factory instead of the concrete class, this fixes incompatibilities with https://github.com/fntneves/laravel-transactional-events
  • updated https://github.com/FriendsOfPHP/PHP-CS-Fixer/releases and have it run on tests too

1.3.3

  • updated tests to better cover Laravel versions after 5.4 and changed the license to MIT

1.3.2

  • updated to work with laravel 5.4.x, only actual changes that aren't style or tests
  • added php-cs-fixer to update the code style
  • added examples to have tests for providers in a full app situation

1.3.1

  • updated to work with laravel 5.x

1.3.0

  • updated to work with laravel 4.2.x, to use in 4.1.x stay on version 1.2.*
  • moved to PSR-4 and now PHP 5.4+
  • small refactor and check isset against 'present' methods, thanks BenConstable

1.2.0

  • presenters can now be nested, thanks alexwhitman
  • added support for using Laravel's View::with(array here), thanks skovachev
  • added ability to use isset(...) and unset(...) on presenter variables, thanks nsbucky
  • added a new decorator for creating the presenter objects. This makes it so you can extend what happens when decorating an object easily

1.1.0

  • the Presenter class now implements ArrayAccess
  • added ability to use an array as your internal data

1.0.2

  • fixed bug caused by laravel update
  • added smarter converting of presenters from PresentableInterface'd objects
  • added object getter getObject to retrieve the internal object

1.0.1

  • fixed bug caused by laravel update

1.0.0

  • Initial Release

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

PHPdecoratorlaravelphppresenter

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category后端框架
PricingOpen source

> Related tools

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架