Web App和API中同样Laravel项目呢? [英] Web app and API in same Laravel project?

查看:136
本文介绍了Web App和API中同样Laravel项目呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出构建一个新的项目,我即将踏上的最佳方式。

I am trying to figure out the best way to structure a new project that I am about to embark on.

我们目前有一个Web应用程序和移动应用程序,它通过一个API都是美联储的数据。目前,Web应用程序是在codeIgniter的安装完成,该API是在codeIgniter单独安装完成。对于数据,Web应用程序,使对API的调用,然后处理返回的数据。

We currently have a web app and a mobile app which are both fed data by an API. Currently the web app is done on an installation of CodeIgniter and the API is done on a separate installation of CodeIgniter. For data, the web app makes calls to the API and then handles the returned data.

在这一点上,它看起来像我们正在做切换到Laravel为Web应用程序,我在想,作为我们的一些规则已经改变它可能是一个很好的机会,也重做API,它可能是时间来关闭codeIgniter干脆。

At this point it looks like we are doing to switch into Laravel for the web app and I was thinking that it might be a good opportunity to also redo the API as some of our rules have changed and it might be time to get off CodeIgniter altogether.

现在的问题:我看过的人建设自己的Web应用程序和API到同一Laravel项目,只是使用路由分别控制API和Web应用程序映射的几个来源。虽然这似乎是它使我不知道如果是因为我想知道是否重API交通会减慢Web应用程序,反之亦然最佳实践一个有趣的想法。

Now for the question: I have read a few sources of people building their web app and API into the same Laravel project and just using the routing to control the mapping of the API and the web app separately. While this seems like an interesting idea it makes me wonder if it is the best practice as I would wonder if heavy API traffic would slow down the web app and vice versa.

问:

请问最好的做法是保持在自己的项目中去耦的API或将是确定做在同一个项目?

Would the best practice be to keep the api decoupled in its own project or would it be OK to do it in the same project?

跟进质询,

若要在Web应用程序的调用通过API如果没有脱钩,也即仅仅是对API路由呼叫?不如通过班去了?

To make a call from the web app through the API if it is not decoupled, would that just be a call to the API route? Better to go through classes?

推荐答案

在我看来,你有两个选择:

In my opinion you have 2 choices:


  • 打造只Laravel的API(响应:: JSON 无处不在),然后用JS框架像角,或jQuery的构建Web应用程序。无论是Web应用程序和移动应用程序可以让AJAX请求到Laravel应用程序,以及JS框架会处理显示出来。这将是未建API + Web应用程序在同一个地方的解决方案。你可以让你的web应用程序内置无论你想,或者Laravel内部应用程序本身。

  • Build only the API with Laravel (Response::json everywhere), then build the web app using a JS framework like Angular, or jQuery. Both the web app and the mobile app could make AJAX requests to the Laravel app, and the JS framework would handle displaying it. This would be the "not building the api + web app in the same place" solution. You could have your web app built wherever you want, or inside the Laravel app itself.

该API是易于维护,你甚至可以合并Web应用程序和移动应用程序,因为它们都做同样的(除非你真的想他们是不同的)。此外,它更容易扩展,因为该接口依然在任何API应用程序的消费者一样。

The API would be easy to maintain, and you could even merge the web app and the mobile app, since they would both do the same (unless you actually want them to be different). Also, it's easier to extend, since the interface remains the same across any API consumer app.

这将有助于很多 https://laracasts.com /系列/增量-API的开发

构建的API,并在同一个地方的Web应用程序,将需要2分离您的应用程序:一部分会响应Ajax请求并返回JSON,另一部分会回答的正常方式,返回PHP的意见和所有..

Building the API and the web app in the same place, would require separating your app in 2: one part would respond to AJAX requests and return JSON and the other part would answer the normal way, returning php views and all that..

这意味着很多额外的工作,并增加一倍的意见的数量,因为你必须建立在移动应用处理的意见,并通过Laravel本身处理的意见。这是一个很多覆盖和维护(及测试)。

This means a lot of extra work and double the amount of views, since you would have to build the views handled by the the mobile app and the views handled by Laravel itself. That's a lot to cover and maintain (and test).

这取决于你正在构建什么样的项目。它是一个SPA?

It depends on what kind of project you're building. Is it a SPA?

这篇关于Web App和API中同样Laravel项目呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆