m1guelpf / laravel-multiformat
Laravel中的多格式端点
v1.0.1
2019-02-07 23:08 UTC
Requires
- php: ^7.1
Requires (Dev)
- larapack/dd: ^1.0
This package is auto-updated.
Last update: 2024-09-23 05:53:31 UTC
README
安装
您可以通过composer安装此包
composer require m1guelpf/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::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']); } ]); } }
贡献
请参阅CONTRIBUTING以获取详细信息。
安全
如果您发现任何与安全相关的问题,请通过电子邮件soy@miguelpiedrafita.com联系,而不是使用问题跟踪器。
鸣谢
许可
MIT许可证(MIT)。请参阅许可文件以获取更多信息。