定制 analogue/presenter 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

analogue/presenter

Composer 安装命令:

composer require analogue/presenter

包简介

Easy view presenters for Analogue ORM

README 文档

README

Based heavily on Jeffrey Way's Easy View Presenters for Laravel

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": {
        "analogue/presenter": "0.2.*"
    }
}

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 Analogue\Presenter\Presenter;

class UserPresenter extends Presenter {

    public function fullName()
    {
        return $this->entity->first . ' ' . $this->entity->last;
    }

    public function accountAge()
    {
        return $this->entity->created_at->diffForHumans();
    }

}

Next, on your entity, pull in the Analogue\Presenter\Presentable trait.

Here's an example - maybe an Analogue User entity.

<?php

use Analogue\ORM\Entity;
use Analogue\Presenter\Presentable;

class User extends Entity {

    use Presentable;
}

Then, add a public $presentable property to the relevant entity map:

<?php

use Analogue\ORM\EntityMap;

// the UserPresenter class we created above
use App\Http\Presenters\UserPresenter;

class UserMap extends EntityMap
{
    public $presenter = UserPresenter::class;

    // ...
}

That's it! You're done. Now, within your controller/view, you can do:

// in some controller
return view("user", ["user" => $user->present()]);
<h1>Hello, {{ $user->fullName() }}</h1>

The Presenter will also pass through any calls to entity properties: e.g. $user->present()->first() would return the $user->first property - this is useful if you pass the presenter, rather than the entity, into your template.

Laravel Integration

There is also a PresentBladeServiceProvider included for use with Laravel and the Blade templating language.

This adds a @presenteach and @endpresenteach directive, which allows you to easily iterate over the presenters for each entity in a collection:

// config/app.php
'providers' => [
    // ...
    Analogue\Presenter\PresentBladeServiceProvider::class,
]
<ul>
@presenteach ($users as $user)
    <li>{{ $user->fullName() }}</li>
@endpresenteach
</ul>

统计信息

  • 总下载量: 658
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 7
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 3
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-07-20

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固