我试图允许用户执行类似 response()->yaml(['their content'])
的操作,但我不明白如何继续注入(inject)我的 YAML 方法到 response() (ResponseFactory) 外观。
是否有任何指南可以解释如何执行此操作?或者也许是某人的快速描述?这是我第一次尝试为 Laravel 构建一个包,它也将是开源的!
我查看了this question ,但不幸的是我没有看到它的用例,并且我不认为它专注于添加通过 response()
调用的附加方法。
最佳答案
您可以使用响应宏
来实现您的目标。
在 AppServiceProvider
的 boot
方法(或在 ServiceProvider 包中)添加以下内容:
Response::macro('yaml', function ($content) {
return yaml_whatever($content); //Use your implementaion here
});
现在,您可以使用return response()->yaml($content);
关于php - 扩展 Laravel 5 的 Response Facade 以包含更多帮助程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38094287/