2017-10-30 03:08
浏览 466


I'm writing a Laravel application using one or more third-party APIs when it suddenly dawned on me. Where is the best place within the Laravel application structure to set up the API connection to consume it from within my Controllers? Would you use a Service or put the logic somewhere else?

$this->api = new RestApi();

图片转代码服务由CSDN问答提供 功能建议

我正在使用一个或多个第三方API编写一个Laravel应用程序,当它突然出现在我身上时。 Laravel应用程序结构中最好的位置是设置API连接以从我的控制器中使用它? 你会使用服务还是把逻辑放在其他地方?

  $ this-> api = new RestApi(); 
  - > setUrl(getenv('API_REST_URL'))
  - > setUsername(getenv('API_USERNAME'))
  - > setPassword(getenv('API_PASSWORD'))
  - > connect(); \  n   
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanjing7459 2018-03-04 20:20

    Using services would be the better approach in Laravel, once your third party is not a direct route or content in your API, per se, it should not be in your Controller. You may want to use a Service with Guzzle.

    点赞 打赏 评论

相关推荐 更多相似问题