doq13207 2016-10-29 16:40
浏览 74
已采纳

如何将Laravel与AngularJS一起使用?

I am trying to use Laravel with AngularJS and want to understand the best way to set up such a project.

Should I (A) have a single domain and consume an API from the Laravel project, or (B) have website.com and api.website.com and consume the API as if it were a 3rd party API?

I can see pros and cons for each, but what I can't get my head around is how routing would work with option A. I assume the initial routing would be via Laravel to display a top level view and then from that point onwards AngularJS would do the routing, but surely AngularJS and its routing are only initialized when the page loads. For example, if a user goes to a subroute without hitting the site root, no route on the Laravel side will exists for that and thus would it not respond with 404/Not Found?

What is the best setup for consuming my Laravel API within AngularJS?

  • 写回答

1条回答 默认 最新

  • duanji1902 2016-10-29 17:11
    关注

    I suggest to separate (your option b).

    • Front and back-end are totally separated
    • You can replace one of them without problems in the other
    • Use middleware, json responses and http status codes
    • Use a framework for back-end too (for example Laravel or Lumen)

    About routes ...

    Your back-end has its own routes (endpoints).

    Your front-end has its own routes (totally different), but should send GET/POST/PUT etc. requests to the back-end. The back-end returns (json) response, which will be parsed by the front-end.

    Develop both separately! So you can use the back-end for third party later.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器