ryannielson/prez
Composer 安装命令:
composer require ryannielson/prez
包简介
Easily create presenters for your objects.
README 文档
README
#Prez
Simple presenters for your PHP or Laravel project.
Why Should I Use Presenters?
Imagine your application has a User model. With Prez, you'd create a matching UserPresenter class. This presenter wraps the model, dealing with only presentational concerns. This keeps any view related logic out of the model, while also helping you keep logic out of the view. In your view you can use the presenter in the same way you'd use the original model. Whenever you start thinking about creating a helper function or adding logic to your view, it might be worth moving it into a presenter.
Installation
Run the following Composer command in your terminal, or simply add "ryannielson/prez": "~1.0.0" to your composer.json file:
composer require ryannielson/prez:'~1.0.0'
Once complete, if using Laravel, you now have to add the the service provider to the providers array in app/config/app.php:
'RyanNielson\Prez\PrezServiceProvider'
Usage
Creating Presenters
Presenters inherit from RyanNielson\Prez\Presenter, and should be named for the model they present. In Laravel these should live in the app/presenters directory.
// app/presenters/UserPresenter.php class UserPresenter extends RyanNielson\Prez\Presenter { }
Laravel Commands
Prez includes a command to automate the creation of Presenters.
To create a presenter called UserPresenter in app/presenters:
php artisan prez:presenter User
To create a presenter called UserPresenter in app/custom:
php artisan prez:presenter User --path=app/custom
Accessing the Object in a Presenter
You can access the wrapped object in a presenter by using $this->object. This allows you get get any public property or call any public function on the object.
class UserPresenter extends RyanNielson\Prez\Presenter { public function fullName() { return $this->object->firstName . ' ' . $this->object->lastName; } }
You can also use the presents property on the presenter so that you can using a name other than $this->object to access the object. This makes the presenter code a bit more clear.
class UserPresenter extends RyanNielson\Prez\Presenter { // This allows us to use $this->user instead of $this->object protected $presents = 'user'; public function fullName() { return $this->user->firstName . ' ' . $this->user->lastName; } }
Getting a Presenter for an Object
By default, Prez assumes that your presenter class uses your object's class name followed by presenter. For example, an object with the class User is assumed to have a corresponding UserPresenter class. The presenter helper method uses this assumption to find your presenter and return an instance of it.
$user = new User; $userPresenter = presenter($user); // Returns an instance of UserPresenter
presenter also takes an optional class name if you want to force a specific presenter to be used.
$user = new User; $userPresenter = presenter($user, 'AdminPresenter'); // Returns an instance of AdminPresenter
If you prefer not to use the helper function, you can explicitily pass your object when constructing a presenter.
$user = new User; $userPresenter = new UserPresenter($user);
Using a Presenter in a View
If you have a presenter object passed to your view, you can use your presenter like any other object. The following example assumes the usage of Laravel's blade template language.
<!-- Assuming you have a $userPresenter available in the view. --> <h1>{{ $userPresenter->fullName() }}</h1>
Delegating to Object
If the presenter doesn't contain a property or method, the call is delegated to the wrapped object. This makes it so that if we want to access a field on the model, we don't have to write a function in the presenter.
class User { public $name = 'Ryan'; public function language() { return 'PHP'; } } class UserPresenter extends RyanNielson\Prez\Presenter { } $userPresenter = new UserPresenter(new User); // Since no method or property exists on presenter, these flow to the object. $userPresenter->name; $userPresenter->language();
ryannielson/prez 适用场景与选型建议
ryannielson/prez 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.41k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2014 年 07 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「view」 「model」 「laravel」 「presenter」 「decorator」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ryannielson/prez 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ryannielson/prez 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ryannielson/prez 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Interfaces for web resource models and services to retrieve and create them
Illuminate View for Morningmedley.
Laravel 5 - Repositories to the database layer
Blade template for Kirby
统计信息
- 总下载量: 2.41k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 13
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2014-07-24