dte49889 2017-09-14 22:59
浏览 69

在Laravel 5.5中使用外部数据

I'm building a site that has to display data collected from an external web service. This service provides multiple data endpoints that are all authenticated against via OAuth.

Rather than each view having to hit an internal method to generate this data (and login to the external service each time) it seems like it'd be much better to create a singleton that will (lazy) connect to the data provider service when a view needing the data is first loaded, and then the various methods supporting the views could just call this same service provider to get the data.

Would this be best in the Laravel world as a service provider? As a helper class with a singleton? Or some other way?

  • 写回答

1条回答 默认 最新

  • doushao8399 2017-09-15 04:36
    关注

    Your Laravel application boots from scratch every time it receives a request (you can cache objects from a previous request, but not an instantiated service), so it won't be possible to create a service that keeps running authenticated between requests (at least with PHP).

    It's not uncommon to create a service and authenticate once per request.

    That being said, you can create a service and bind it to the container using $app->bind() or $app->singleton().

    Docs about it here: https://laravel.com/docs/5.5/container

    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路