[英]How to add custom method to Lumen's response class
I'm using response method withHeaders()
: 我正在使用
withHeaders()
响应方法:
return response(view('pages.top.index', compact('data')))->withHeaders(['one-param' => 'data', 'second-param' => 'data2' ...);
And I have multiple same params that I put in withHeaders()
method almost in every Controller's action. 而且我几乎在每个Controller的动作中都放入了
withHeaders()
方法的多个相同参数。 Is there a way I can add my own method and chain it like: 有没有一种方法可以添加我自己的方法并将其链接为:
return response(view('pages.top.index', compact('data')))->customMethod('data', 'data2', ....);
Response is macroable so you can add this to the service provider: 响应是可宏的,因此您可以将其添加到服务提供商:
\Illuminate\Http\Response::macro('customMethod', function () {
//Method body
return $this; //To chain it
});
Note: I tend to avoid this because it gives my IDE a very hard time with type-hinting . 注意:我倾向于避免这种情况,因为它给我的IDE带来了非常麻烦的类型提示。
If the problem is with needing to pass the same data over and over again you might also consider sharing data with all views 如果问题在于需要一遍又一遍地传递相同的数据,您也可以考虑与所有视图共享数据
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.