Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Multiformat Endpoints in Laravel
<?php
namespace App\Providers;
use Illuminate\Http\Request;
use Illuminate\Routing\Route;
use Illuminate\Support\ServiceProvider;
use App\Http\Middleware\CaptureRequestExtension;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Route::macro('multiformat', function () {
return $this->setUri($this->uri() . '.{_format?}');
});
Request::macro('match', function ($responses, $defaultFormat = 'html') {
if ($this->route('_format') === null) {
return value(array_get($responses, $this->format($defaultFormat)));
}
return value(array_get($responses, $this->route('_format'), function () {
abort(404);
}));
});
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
}
<?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::match()` 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()->match([
'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']);
}
]);
}
}
@vtalbot

This comment has been minimized.

Copy link

vtalbot commented Feb 15, 2018

Shouldn't the 'html' in 3-use-it.php line 29 return a closure like xml to avoid useless processing?

BTW, really awesome example!

@markhuot

This comment has been minimized.

Copy link

markhuot commented Feb 16, 2018

@vtalbot, the view() will automatically become a 200 text/html reponse. Without the closure the XML response would do the same. The closure ensures the XML response has the correct Content-type.

It would be over engineered, but fun, if the response could automatically see a SimpleXML implementation, or some other signifier, and set the content type automatically. That’d be a nice reason to fork this.

@BrandonSurowiec

This comment has been minimized.

Copy link

BrandonSurowiec commented Feb 17, 2018

You can probably shorten the macro to this:

        Request::macro('match', function ($responses, $defaultFormat = 'html') {
            return value(array_get($responses, $this->route()->parameter('_extension') ?? $this->format($defaultFormat), function () {
                abort(404);
            }));
        });
@m1guelpf

This comment has been minimized.

Copy link

m1guelpf commented Feb 17, 2018

Extracted to a package, now you can just do:

composer require m1guelpf/laravel-multiformat
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.