Laravel Presenter 在 Controller 的使用

Laravel PHP Framework

Laravel Auto Presenter 是一套用在 view 裡的 decorates objects,搭配資料庫時,如果有需要組合或整合欄位來顯示相關資訊,這套就非常適合使用在 Laravel View 裡,如果不是透過 Laravel Auto Presenter,開發者也可以利用 Laravel Accessors & Mutators 來實現這方法,只是這要寫在 Model 層,寫法如下,此做法寫起來蠻亂的,而且也並不是每個地方都需要擴充這些欄位。

/**
 * The accessors to append to the model's array form.
 *
 * @var array
 */
protected $appends = [
    'is_twitter',
];

/**

  • Get the user’s is_twitter flag.
  • @param string $value
  • @return string */ public function getIsTwitterAttribute() { return (bool) ($this->attributes[‘options’] & self::$OPTIONS[‘is_twitter’]); }

如果是使用 Laravel Auto Presenter 則寫法如下

use App\Models\User;
use Illuminate\Contracts\Support\Arrayable;
use McCool\LaravelAutoPresenter\BasePresenter;

class UserPresenter extends BasePresenter implements Arrayable { public function __construct(User $resource) { $this->wrappedObject = $resource; }

public function isTwitter()
{
    return (bool) ($this->wrappedObject->info->options & self::$OPTIONS['is_twitter']);
}

/**
 * Get the instance as an array.
 *
 * @return array
 */
public function toArray()
{
    return [
        'id'        => $this->wrappedObject->id,
        'isTwitter' => $this->isTwitter(),
        'avatarUrl' => $this->avatarUrl(),
    ];
}

}

但是使用在 Laravel View 裡面都可以正確拿到 isTwitter 欄位沒問題,如果要用在 RESTFul + 前端 ReactJS 的話,則要透過 Laravel Facades 來實現,加上 AutoPresenter::decorate 就可以在 Controller 內使用 AutoPresenter 了。

use McCool\LaravelAutoPresenter\Facades\AutoPresenter;

class MessageController extends Controller { /** * @var Repository */ private $user;

public function __construct(UserRepository $user)
{
    $this->user = $user;
}

public function index(Request $request)
{
    $user = $this->user->with('info')->find(\Auth::id());
    $state = collect([
        'user' => AutoPresenter::decorate($user),
    ]);

    return $state;
}

}


See also