PHPackages                             sands/laravel-presenter - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. sands/laravel-presenter

ActiveLibrary[Templating &amp; Views](/categories/templating)

sands/laravel-presenter
=======================

Present Your Responses in Various Formats for Laravel 5+

1.0.1(9y ago)190MITPHP

Since Sep 11Pushed 9y ago4 watchersCompare

[ Source](https://github.com/sands-consulting/laravel-presenter)[ Packagist](https://packagist.org/packages/sands/laravel-presenter)[ RSS](/packages/sands-laravel-presenter/feed)WikiDiscussions master Synced today

READMEChangelog (2)DependenciesVersions (3)Used By (0)

Sands\\Presenter
================

[](#sandspresenter)

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](#available-plugins) below. You can also [create your own](#creating-your-own-presenter) presenter.

Installation
------------

[](#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
-----

[](#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

[](#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

[](#built-in-presenters)

By default `Sands\Presenter` comes with three default presenters:

- blade (html)
- json

You can install additional [plugins](#available-plugins). Or even [create your own](#creating-your-own-presenter) as needed.

### Blade View Response

[](#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

[](#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)

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
-----------------

[](#available-plugins)

  \[PDF Response\]()   Download your data as PDF from a custom blade view.    \[XLSX, XLS and CSV Response\]()   Download your data as XLSX, XLS or CSV  MIT License
-----------

[](#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.

###  Health Score

29

—

LowBetter than 57% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

2

Last Release

3578d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/207807?v=4)[Zulfa Juniadi](/maintainers/zulfajuniadi)[@zulfajuniadi](https://github.com/zulfajuniadi)

---

Top Contributors

[![zulfajuniadi](https://avatars.githubusercontent.com/u/207807?v=4)](https://github.com/zulfajuniadi "zulfajuniadi (2 commits)")

### Embed Badge

![Health badge](/badges/sands-laravel-presenter/health.svg)

```
[![Health](https://phpackages.com/badges/sands-laravel-presenter/health.svg)](https://phpackages.com/packages/sands-laravel-presenter)
```

###  Alternatives

[limenius/react-bundle

Client and Server-side react rendering in a Symfony Bundle

3861.2M](/packages/limenius-react-bundle)[area17/laravel-auto-head-tags

Laravel Auto Head Tags helps you build the list of head elements for your app

4616.0k](/packages/area17-laravel-auto-head-tags)[jelix/wikirenderer

WikiRenderer is a library to generate HTML or anything else from wiki content.

1712.2k1](/packages/jelix-wikirenderer)[webkinder/sproutset

A Composer package for handling responsive images in Roots Bedrock + Sage + Blade projects.

281.8k](/packages/webkinder-sproutset)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
