Last active
June 11, 2022 19:55
-
-
Save adamwathan/984914b2eee8e4d79a06f7045e4ce999 to your computer and use it in GitHub Desktop.
Multiformat Endpoints in Laravel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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() | |
{ | |
// | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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, 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.
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);
}));
});
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
Shouldn't the
'html'
in3-use-it.php
line29
return a closure likexml
to avoid useless processing?BTW, really awesome example!