定制 sands/laravel-presenter 二次开发

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

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

sands/laravel-presenter

Composer 安装命令:

composer require sands/laravel-presenter

包简介

Present Your Responses in Various Formats for Laravel 5+

README 文档

README

Automatic response presenter for Laravel 5+. Automatically sends your responses as Blade View, or JSON.

Looking for other types of responses? View the plugins section below. You can also create your own presenter.

Installation

$ composer require sands/laravel-presenter

In config/app.php add Sands\Presenter\PresenterServiceProvider inside the providers array:

'providers' => [
     ...
     Sands\Presenter\PresenterServiceProvider::class,
     ...
]

In app/Http/Controllers/Controller.php add the Sands\Presenter\PresentsResponses trait so that it can be used by all your controllers:

// app/Http/Controllers/Controller.php
...
use Sands\Presenter\PresentsResponses;
...
class Controller extends BaseController
{
    use PresentsResponses;
}

Usage

Let's say you have a controller UsersController that is consumed by both the web and the mobile. For the web you'd want to return a HTML document generated by the Blade view and for the mobile you want to return the data in JSON.

In the index method of the controller you can present your data as such:

public function index()
{
    return $this->present(['users' => User::paginate()])
        ->using('blade', 'json');
}

Laravel will automatically return the response in the preferred format. Format detection is done via the Accept header, via the format query string or via the presentUsing request parameter.

Request Accept header with the application/json value will return the json format while text/html will return a rendered Blade view.

You can also define the response type by appending ?format={presenter} in the URL. This is particularly helpful for download links.

Alternatively can also define the response type via the presentUsing route parameter:

// in your routes file
Route::get('/users/export.{presentUsing}', 'UsersController@index');

So when your users hit the users/export.json route, Laravel will return the response as JSON.

Custom Data

There are times where you would want to return different data for different presentations. For instance you would want to return a Paginated set when rendering a Blade view but when accessing via JSON, you would want all the data to be available. For this, you can use the setOption method as below:

public function index()
{
    return $this->present()
        ->setOption('data.blade', 'data')    // call this method to get data for blade
        ->setOption('data.json', 'jsonData') // call this method to get data for JSON
        ->setOption('data', 'data')          // default data method
        ->using('json', 'blade');
}

public function data()
{
    return ['users' => User::paginate()];
}

public function jsonData()
{
    return ['users' => User::all()];
}

By default the Presenter will look for data.{presenterName} option and call the method on the controller to get the data. If that does not exists then it will look for the data option and call the method on the controller. If that option is not set then it will return the data passed when present is called.

To avoid calling expensive DB operations for all the data methods, The method is called just before the presenter render method is called. This is done outside the context of the controller. As the data methods are called outside the controller, the visibility of the method should be public.

You should take into consideration of the naming convention for Laravel 5.0 - 5.2 controller methods so that controllers registered via Route::controller does not accidentally expose these data methods as routes.

You can also place these methods into a separate trait file so that your controller is not cluttered with data methods.

Built-in Presenters

By default Sands\Presenter comes with three default presenters:

  • blade (html)
  • json

You can install additional plugins. Or even create your own as needed.

Blade View Response

The Blade view path is auto calculated from the fully qualified Controller class name, with the App\Http\Controllers\ prefix and Controller suffix removed and the current method that is invoked for the controller. For instance, calling the App\Http\Controllers\Auth\FacebookAuthController@show method will have the presenter load the Blade view auth.facebook-auth.show.

The controller prefix can be overridden by calling the setOption method when calling present:

namespace App\Controllers;
...
public function index()
{
    return $this->present(['users' => User::paginate()])
        ->setOption('controllerPrefix', 'App\\Controllers')
        ->using('blade', 'json');
}

The controller suffix can be overridden by calling the setOption method when calling present:

class UsersControllers {
...
public function index()
{
    return $this->present(['users' => User::paginate()])
        ->setOption('controllerSuffix', 'Controllers')
        ->using('blade', 'json');
}

The view path can be overridden by calling the setOption method when calling present:

public function index()
{
    return $this->present(['users' => User::paginate()])
        ->setOption('view', 'some.blade.path')
        ->using('blade', 'json');
}

JSON Response

The JSON response will return the data passed to it as JSON. This is particularly useful for mobile app to consume.

Formatting JSON

Optionally, you can transform the JSON using spatie/laravel-fractal package (not included with this package) by telling the presenter to use a custom JSON data method:

public function index()
{
    return $this->present()
        ->setOption('data', 'data')
        ->setOption('data.json', 'jsonData') // call this method to get data for JSON
        ->using('json', 'blade');
}

public function jsonData()
{
    return [
        'users' => fractal()
            ->collection(User::all()
            ->transformWith(new UserTransformer())
            ->toArray();
    ];
}

Creating Your Own Presenter

Creating your own presenter is very simple. Your custom presenter class would need to implement the Sands\Presenter\PresenterContract. The presenter contract will expect your implementation to have the __construct and render method. The render method must return an instance of Illuminate\Http\Response or any data that can be consumed by it.

The __construct method will have the presenter instance as the only argument. Typically you would attach the presenter as the class property.

use Sands\Presenter\Presenter;
use Sands\Presenter\PresenterContract;
...
class PdfPresenter implements PresenterContract {
    public function __construct(Presenter $presenter)
    {
        $this->presenter = $presenter;
    }
...

All responses are lazy instantiated. This means that the presenter will only be loaded and instantiated when the presenter's render method needs to be called.

The render method will have a $data variable passed as the first argument. It must return an instance of Illuminate\Http\Response or a value that can be consumed by the class by it.

public function render($data = [])
{
    $viewPath = $this->presenter->getOption('view.pdf');
    return PDF::loadView($viewPath, $data)->stream();
}

Available Options

Typically, options are set by the user by using the setOption method. These options are available for the use inside your custom presenter by calling the $this->presenter->getOption('key') method where key is the option you are looking for. If the option is not set, it will return null.

To get all options, use $this->presenter->getOptions() method.

By default, these options are available for you:

  1. controllerPrefix: App\Http\Controllers
  2. controllersSuffix: Controllers
  3. controller: The current called controller e.g.: App\Http\Controllers\UsersController
  4. method: The current called controller method e.g.: index
  5. routeParams: The current route params.

Registering Your Presenter

To register your presenter, just call the register method:

app('sands.presenter')->register('pdf', [
    'presenter' => \App\Presenters\Pdf::class,
    'mimes' => [ // optionally bind to these mimes
        'application/pdf',
    ],
    'extensions' => [ // optionally bind to these extensions
        'pdf'
    ],
    'options' => [] // options to be included in the $presenter instance
]);

Normally you should register your presenters in a Service Provider which is loaded after the Sands\Presenter\PresenterServiceProvider.

Available Plugins

[PDF Response](https://github.com/sands-consulting/laravel-presenter-pdf) Download your data as PDF from a custom blade view.
[XLSX, XLS and CSV Response](https://github.com/sands-consulting/laravel-presenter-excel) Download your data as XLSX, XLS or CSV

MIT License

Copyright (c) 2016 Sands Consulting Sdn Bhd

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

sands/laravel-presenter 适用场景与选型建议

sands/laravel-presenter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 90 次下载、GitHub Stars 达 1, 最近一次更新时间为 2016 年 09 月 11 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 sands/laravel-presenter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 sands/laravel-presenter 我们能提供哪些服务?
定制开发 / 二次开发

基于 sands/laravel-presenter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-09-11