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.

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

报告相同问题?

悬赏问题

  • ¥50 adb连接不到手机是怎么回事?
  • ¥15 vs2022无法联网
  • ¥15 TCP的客户端和服务器的互联
  • ¥15 VB.NET操作免驱摄像头
  • ¥15 笔记本上移动热点开关状态查询
  • ¥85 类鸟群Boids——仿真鸟群避障的相关问题
  • ¥15 CFEDEM自带算例错误,如何解决?
  • ¥15 有没有会使用flac3d软件的家人
  • ¥20 360摄像头无法解绑使用,请教解绑当前账号绑定问题,
  • ¥15 docker实践项目