phl/laravel-respond-to
Composer 安装命令:
composer require phl/laravel-respond-to
包简介
Provide rails like respond_to functionality to Laravel
README 文档
README
This library will provide you with rails like respond_to functionality. This
feature allows a controller action to return different responses according to
the request Accept header (the format).
Installation
composer require phl/laravel-respond-to
Usage
You may use the respond to functionality in any of your controller action by
using the PHL\LaravelRespondTo\Respond class.
<?php use PHL\LaravelRespondTo\Respond; class MyController { public function index() { return Respond::to('html')->withView('hello', ['name' => 'John Doe']) ->to('json')->with(['key' => 'value']) ->to('txt')->with('some text') ->to('xml')->with(function () {}) ->to('rss')->with(new \Illuminate\Http\Response()) ->to('css')->with(new ResponsableClass); } }
This controller is now able to respond to different formats with different responses.
Each call to the to function must be followed by a with or withView
call.
Default format
If the current request asks for a response format that is not supported the default one will be returned.
The default format is html you may change that using the default function.
use PHL\LaravelRespondTo\Respond; Respond::withNewRespond() ->to('json')->with(['key' => 'value']) ->default('json');
If no response has been set for the default format an exception will get thrown when the controller will try to resolve the response
Supported formats
Under the hood this library relies on the Symfony Request mime types list. This list is initially set with these formats:
$formats = [ 'html' => ['text/html', 'application/xhtml+xml'], 'txt' => ['text/plain'], 'js' => ['application/javascript', 'application/x-javascript', 'text/javascript'], 'css' => ['text/css'], 'json' => ['application/json', 'application/x-json'], 'jsonld' => ['application/ld+json'], 'xml' => ['text/xml', 'application/xml', 'application/x-xml'], 'rdf' => ['application/rdf+xml'], 'atom' => ['application/atom+xml'], 'rss' => ['application/rss+xml'], 'form' => ['application/x-www-form-urlencoded'], ];
You may add new formats to this list like so:
request()->setFormat('csv', 'text/csv');
Writing the first to on a new line.
If like me you'd prefer to have each call to the to function on its own line
I've added a bit of syntactic sugar with the withNewRespond function.
use PHL\LaravelRespondTo\Respond; Respond::withNewRespond() ->to('html')->with('some html') ->to('json')->with(['key' => 'value']) ->to('xml')->withView('welcome_xml');
Contributing and help
If you have any questions about how to use this library feel free to open an issue :)
If you think that the documentation or the code could be improved open a PR and I'll happily review it!
phl/laravel-respond-to 适用场景与选型建议
phl/laravel-respond-to 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 575 次下载、GitHub Stars 达 1, 最近一次更新时间为 2019 年 04 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 phl/laravel-respond-to 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 phl/laravel-respond-to 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 575
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-04-16