同一个 Laravel 项目中的 Web 应用程序和 API? [英] Web app and API in same Laravel project?

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

问题描述

我正在努力找出构建我即将开始的新项目的最佳方式.

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

我们目前有一个网络应用和一个移动应用,它们都由 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.

此时看起来我们正在为 web 应用程序切换到 Laravel,我认为这可能是一个重做 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 流量是否会降低网络应用的速度,反之亦然.

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(Response::json 无处不在),然后使用 Angular 或 jQuery 等 JS 框架构建 Web 应用程序.Web 应用程序和移动应用程序都可以向 Laravel 应用程序发出 AJAX 请求,并且 JS 框架会处理显示它.这将是不在同一个地方构建 api + 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/series/incremental-api-development

在同一个地方构建 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?

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

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