定制 lumenated/fractal-views 二次开发

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

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

lumenated/fractal-views

Composer 安装命令:

composer require lumenated/fractal-views

包简介

A view abstraction for fractal

README 文档

README

A view abstraction for Fractal making it more simple to integrate inside a framework.

fractal views Build Status Coverage Status Packagist Packagist

Installation

composer require lumenated/fractal-views

Configuration

In order to use Fractal views for a resource we need to implement a transformer to map the resource to a serializable array and a view to present our resource to the consumer. A View extends the Lumenated\FractalViews\Views class which exposes two methods:

  • renderOne which renders a single resource to an array
  • renderMany which renders multiple objects to an array with pagination support
class BookView extends Lumenated\FractalViews\View 
{
  // The fractal transformer that has to be used for this view
  protected $transformerClass = BookTransformer::class;
}

Next up we need to implement the BookTransformer. Below is an example from the fractal documentation:

<?php
namespace Acme\Transformer;

use Acme\Model\Book;
use League\Fractal;

class BookTransformer extends Fractal\TransformerAbstract
{
	public function transform(Book $book)
	{
	    return [
	        'id'      => (int) $book->id,
	        'title'   => $book->title,
	        'year'    => (int) $book->yr,
            'links'   => [
                [
                    'rel' => 'self',
                    'uri' => '/books/'.$book->id,
                ]
            ],
	    ];
	}
}

Usage

After configuring our view they can be used inside our project. Below is an example how to use them in a Lumen controller:

namespace \App\Http\Controllers;

class BookController extends Controller 
{
  private $view;
  
  public function __construct(BookView $view) 
  {
    $this->view = $view;
    
  }
  
  public function get($id) 
  {
    $book = Book::findOrFail($id);
    
    return response()->json($this->view->render($book));
  }
  
  public function getAll() 
  {
    $books = Book::all();
    
    return response()->json($this->view->render($books));
  }
}

Running tests

after installing the dependencies with:

composer install

Execute the following command to execute the tests:

vendor/bin/phpunit

统计信息

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

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2017-03-24

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固