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.

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

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?