ryanwinchester / laravel-auto-presenter
一个用于自动装饰模型为展示对象的系统。
Requires
- php: >=5.5.9
- illuminate/container: 5.1.*|5.2.*|5.3.*
- illuminate/contracts: 5.1.*|5.2.*|5.3.*
- illuminate/events: 5.1.*|5.2.*|5.3.*
- illuminate/pagination: 5.1.*|5.2.*|5.3.*
- illuminate/support: 5.1.*|5.2.*|5.3.*
- illuminate/view: 5.1.*|5.2.*|5.3.*
Requires (Dev)
- graham-campbell/testbench: ^3.1
- mockery/mockery: ^0.9.4
- phpunit/phpunit: ^4.8|^5.0
This package is auto-updated.
Last update: 2024-09-13 08:08:10 UTC
README
此包在视图渲染过程中自动装饰绑定到视图的对象。
特性
- 自动装饰绑定到视图的对象
- 自动装饰分页实例内的对象
- 自动装饰数组和集合内的对象
安装
需要 PHP 5.5+ 或 HHVM 3.6+。
要获取 Laravel Auto Presenter 的最新版本,只需使用 Composer 包管理器引入项目
$ composer require ryanwinchester/laravel-auto-presenter
当然,您也可以手动更新您的 require 块并运行 composer update
{ "require": { "ryanwinchester/laravel-auto-presenter": "^4.0" } }
然后,在您的 config/app.php
文件中,将此行添加到 'providers' 数组中。
'McCool\LaravelAutoPresenter\AutoPresenterServiceProvider',
使用方法
为了展示如何使用,我们假设我们有一个 Eloquent Post 模型。它不一定是 Eloquent,可以是任何类型的类。但这是一个正常的情况。Post 模型代表一个博客文章。
这里使用的是非常基础的代码示例,所以请只关注如何使用自动展示器,而忽略其他部分。
use Example\Accounts\User; use Illuminate\Database\Eloquent\Model; class Post extends Model { protected $table = 'posts'; protected $fillable = ['author_id', 'title', 'content', 'published_at']; public function author() { return $this->belongsTo(User::class, 'author_id'); } }
同时,我们还需要一个控制器...
use Example\Accounts\Post; use Illuminate\Routing\Controller; use Illuminate\Support\Facades\View; class PostsController extends Controller { public function getIndex() { $posts = Post::all(); return View::make('posts.index', compact('posts')); } }
和视图...
@foreach($posts as $post) <li>{{ $post->title }} - {{ $post->published_at }}</li> @endforeach
在这个例子中,published_at 属性可能以 "Y-m-d H:i:s" 或 "2013-08-10 10:20:13" 的格式存在。在现实世界中,我们不希望在视图中看到这种格式。所以,让我们创建一个展示器,让我们能够改变 Post 类的数据在视图中的显示方式。
use Carbon\Carbon; use Example\Accounts\Post; use McCool\LaravelAutoPresenter\BasePresenter; class PostPresenter extends BasePresenter { public function __construct(Post $resource) { $this->wrappedObject = $resource; } public function published_at() { $published = $this->wrappedObject->published_at; return Carbon::createFromFormat('Y-m-d H:i:s', $published) ->toFormattedDateString(); } }
在这里,自动展示器装饰器正在注入要装饰的 Post 模型。**请注意,构造函数参数应始终命名为 $resource
,以便 Laravel 的 IoC 容器正确解析依赖关系**。
我们需要 post 类实现该接口。
use Example\Accounts\User; use Example\Blog\PostPresenter; use McCool\LaravelAutoPresenter\HasPresenter; use Illuminate\Database\Eloquent\Model; class Post extends Model implements HasPresenter { protected $table = 'posts'; protected $fillable = ['author_id', 'title', 'content', 'published_at']; public function author() { return $this->belongsTo(User::class, 'author_id'); } public function getPresenterClass() { return PostPresenter::class; } }
现在,没有任何额外的更改,我们的视图将以所需的格式显示日期。
故障排除
如果对象在视图中没有正确装饰,那么它很可能在视图开始渲染时并不存在。例如,延迟加载的关系不会被装饰。您可以通过预加载它们来修复此问题。Auth::user() 永远不会被装饰。我更喜欢将 $currentUser 绑定到我的视图中。
安全
如果您在此包中发现安全漏洞,请向 Graham Campbell 发送电子邮件至 graham@alt-three.com。所有安全漏洞都将得到及时处理。
许可证
Laravel Auto Presenter 使用 MIT 许可证 (MIT) 许可。