Download the PHP package sands/laravel-presenter without Composer
On this page you can find all versions of the php package sands/laravel-presenter. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download sands/laravel-presenter
More information about sands/laravel-presenter
Files in sands/laravel-presenter
Package laravel-presenter
Short Description Present Your Responses in Various Formats for Laravel 5+
License MIT
Informations about the package laravel-presenter
Sands\Presenter
Automatic response presenter for Laravel 5+. Automatically sends your responses as Blade View, or JSON.
Looking for other types of responses? View the create your own presenter.
Installation
In config/app.php
add Sands\Presenter\PresenterServiceProvider
inside the providers
array:
In app/Http/Controllers/Controller.php
add the Sands\Presenter\PresentsResponses
trait so that it can be used by all your controllers:
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:
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:
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:
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 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
:
The controller suffix can be overridden by calling the setOption
method when calling present
:
The view path can be overridden by calling the setOption
method when calling present
:
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:
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.
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.
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:
controllerPrefix
:App\Http\Controllers
controllersSuffix
:Controllers
controller
: The current called controller e.g.:App\Http\Controllers\UsersController
method
: The current called controller method e.g.:index
routeParams
: The current route params.
Registering Your Presenter
To register your presenter, just call the register
method:
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 |