laracasts/Presenter
소개
앱의 뷰에 프레젠터를 간편하게 추가할 수 있습니다.
기타 프로젝트측정된 스타 증가
- in 24h
- 0
- in 7 days
- -2
- in 15 days
- -2
- in 1 month
- 기록 수집 중
서로 겹치는 기간의 순증가량이며 일별 합계가 아닙니다. 측정값이 없으면 비워 둡니다. 스타는 관심도를 나타내며 품질을 보장하지 않습니다.
README.md
OriginalEasy View Presenters
So you have those scenarios where a bit of logic needs to be performed before some data (likely from your entity) is displayed from the view.
- Should that logic be hard-coded into the view? No.
- Should we instead store the logic in the model? No again!
Instead, leverage view presenters. That's what they're for! This package provides one such implementation.
Install
Pull this package in through Composer.
{
"require": {
"laracasts/presenter": "0.1.*"
}
}
Usage
The first step is to store your presenters somewhere - anywhere. These will be simple objects that do nothing more than format data, as required.
Here's an example of a presenter.
use Laracasts\Presenter\Presenter;
class UserPresenter extends Presenter {
public function fullName()
{
return $this->first . ' ' . $this->last;
}
public function accountAge()
{
return $this->created_at->diffForHumans();
}
}
Next, on your entity, pull in the Laracasts\Presenter\PresentableTrait trait, which will automatically instantiate your presenter class.
Here's an example - maybe a Laravel User model.
<?php
use Laracasts\Presenter\PresentableTrait;
class User extends \Eloquent {
use PresentableTrait;
protected $presenter = 'UserPresenter';
}
That's it! You're done. Now, within your view, you can do:
<h1>Hello, {{ $user->present()->fullName }}</h1>
Notice how the call to the present() method (which will return your new or cached presenter object) also provides the benefit of making it perfectly clear where you must go, should you need to modify how a full name is displayed on the page.
Have fun!
Jeffrey @ https://laracasts.com
관련 프로젝트 살펴보기
불러오는 중…