ptournet/laravel-multiformat is a Laravel package for multiformat endpoints in laravel.
It currently has 0 GitHub stars and 11 downloads on Packagist (latest version v1.1.1).
Install it with composer require ptournet/laravel-multiformat.
Discover more Laravel packages by ptournet
or browse all Laravel packages to compare alternatives.
Last updated
This package allows a single Laravel route to answer with different formats (often HTML and JSON). It is meant to be a drop-in replacement fo the unmaintained package: m1guelpf/laravel-multiformat with new features and compatibility with the latest Laravel versions.
You can install the package via composer:
composer require ptournet/laravel-multiformat
<?php
/**
* Mark a route as 'multiformat' to allow different extensions (html, json, xml, etc.)
*
* This route will match all of these requests:
* /podcasts/4
* /podcasts/4.json
* /podcasts/4.html
* /podcasts/4.zip
*/
Route::get('/podcasts/{id}', 'PodcastsController@show')->multiformat();
/**
* Use `Request::matchFormat()` to return the right response for the requested format.
*
* Supports closures to avoid doing unnecessary work, and returns 404 if the
* requested format is not supported.
*
* Will also take into account the `Accept` header if no extension is provided.
*/
class PodcastsController
{
public function show($id)
{
$podcast = Podcast::findOrFail($id);
return request()->matchFormat([
'html' => view('podcasts.show', [
'podcast' => $podcast,
'episodes' => $podcast->recentEpisodes(5),
]),
'json' => $podcast,
'xml' => function () use ($podcast) {
return response($podcast->toXml(), 200, ['Content-Type' => 'text/xml']);
}
]);
}
}
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email ptournet (at sign) gmail.com instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.