使用演示工具来装饰对象。主要是为了将演示逻辑保持在模型之外。
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).
composer require robclancy/presenter:^2.0or
Add robclancy/presenter to the "require" section of your composer.json file.
"robclancy/presenter": "^2.0"Run composer update to get the latest version of the package.
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.
// 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.
laravel 5.4.x, only actual changes that aren't style or testslaravel 5.xlaravel 4.2.x, to use in 4.1.x stay on version 1.2.*isset against 'present' methods, thanks BenConstableView::with(array here), thanks skovachevisset(...) and unset(...) on presenter variables, thanks nsbuckygetObject to retrieve the internal object暂无开放 Issues,或尚未同步最近议题。