Skip to content

Instantly share code, notes, and snippets.

@adamwathan
Last active June 11, 2022 19:55
Show Gist options
  • Star 97 You must be signed in to star a gist
  • Fork 16 You must be signed in to fork a gist
  • Save adamwathan/984914b2eee8e4d79a06f7045e4ce999 to your computer and use it in GitHub Desktop.
Save adamwathan/984914b2eee8e4d79a06f7045e4ce999 to your computer and use it in GitHub Desktop.
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
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
Copy link

@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
Copy link

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

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