lewis/presenter
Composer 安装命令:
composer require lewis/presenter
包简介
Simple view presenter library for Laravel.
README 文档
README
A view presenter is an incredibly useful way of decorating objects bound to your views. This allows you to perform view related logic in a sensible place instead of placing it directly in your views, or worse, your models.
There are several other libraries out their that provide similar functionality. This package is merely my preferred implementation as everything is configured in isolation to the object being decorated.
Install
You can install this package using Composer:
$ composer require lewis/presenter
Or in your composer.json:
{
"require": {
"lewis/presenter": "0.1.*"
}
}
Once you've run composer update you'll need to register the service provider in your config/app.php file.
'providers' => [ Lewis\Presenter\PresenterServiceProvider::class ]
Usage
Configuring Presenters
There's several ways to configure your presenters. First, you can utilize the configuration file, which can be published using the following command:
$ php artisan vendor:publish --provider="Lewis\Presenter\PresenterServiceProvider"
There are no presenters configured by default. The published file merely contains an explanation on how to configure your presenters. You must provide an array of key/value pairs linking your object to its presenter.
return [ App\User::class => App\Presenters\UserPresenter::class, App\Post::class => App\Presenters\PostPresenter::class ];
If you'd prefer you can set an array of presenters directly on the decorator. You might choose do to this from a service provider.
$this->app['decorator']->setBindings([ \App\User::class => \App\Presenters\UserPresenter::class, \App\Post::class => \App\Presenters\PostPresenter::class ]);
Lastly, you can configure presenters one at a time using the register method, again, from within a provider.
$this->app['decorator']->register(\App\User::class, \App\Presenters\UserPresenter::class);
Creating Presenters
A presenter should extend from Lewis\Presenter\AbstractPresenter, however, it is NOT required, but highly recommended, as you'll have access to several
methods and magic methods that provide some useful functionality.
I like to keep my presenters within a Presenters folder, however, you may organize things in whichever way you prefer.
namespace App\Presenters; use Lewis\Presenter\AbstractPresenter; class UserPresenter extends AbstractPresenter { }
If you wish to inject dependencies the only requirements are that you name a parameter $object so that Laravel can correctly inject the bound object and that
you call the parent constructor.
namespace App\Presenters; use App\SomeNamespace\SomeClass; use Lewis\Presenter\AbstractPresenter; class UserPresenter extends AbstractPresenter { protected $class; public function __construct($object, SomeClass $class) { $this->class = $class; parent::__construct($object); } }
Your presenter can then define methods to perform logic that can be used in your views.
public function prettySlug() { return '/'.ltrim($this->slug, '/'); }
You can reference properties on the wrapped object directly (as above), or by using the $object property.
public function prettySlug() { return '/'.ltrim($this->object->slug, '/'); }
From Within Views
Now that you've configured your presenters and created them, you just need to use them from within your views. It's a simple matter of calling the method or property as you define it.
{{ $post->prettySlug }}
Or:
{{ $post->prettySlug() }}
You can also still access you relations and other model attributes.
{{ $post->title }}
@foreach($post->comments as $comment)
...
@endforeach
Enjoy
That's about all there is to it.
lewis/presenter 适用场景与选型建议
lewis/presenter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.66k 次下载、GitHub Stars 达 16, 最近一次更新时间为 2015 年 09 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「view」 「laravel」 「presenter」 「decorator」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 lewis/presenter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 lewis/presenter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 lewis/presenter 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
A nestable user interface building module for the Rhubarb PHP framework based upon the model-view-presenter pattern.
Implementation for Laravel of the presenter design pattern.
Presentation and Formatting helper with nice fluent interface.
Illuminate View for Morningmedley.
Nette presenter tree
统计信息
- 总下载量: 4.66k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 17
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-09-07