我是否需要一个框架来在 PHP 中构建 REST API?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11268933/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:08:53  来源:igfitidea点击:

Do I need a framework to build a REST API in PHP?

phpapirestframeworks

提问by Progger

I am fairly new to PHP (about 8 months). I am building a web app, which is almost ready for beta. I am only now starting to think about what I would need to do to make a mobile version of the app.

我对 PHP 相当陌生(大约 8 个月)。我正在构建一个网络应用程序,它几乎准备好进行测试。我现在才开始考虑制作该应用程序的移动版本需要做什么。

As I understand, I should be building a REST API (please correct me if I'm wrong). I am currently NOT using a PHP framework for my web app. Should I be?

据我了解,我应该构建一个 REST API(如果我错了,请纠正我)。我目前没有为我的 Web 应用程序使用 PHP 框架。我可以做?

Should I now begin using a framework so that I could more easily implement my API? Or can I build my API without any framework at all?

我现在应该开始使用框架以便更轻松地实现我的 API 吗?或者我可以在没有任何框架的情况下构建我的 API 吗?

采纳答案by Napolux

SHORT ANSWERNo, you don't need a framework to achieve your goal.

简短回答不,您不需要框架来实现您的目标。

BUTit will be really easier if you use a framework to manage your API. I suggest you to go for a lightweight framework and maybe you can convert easily your webapp to the framework too, having one "app" to return two different "things" (web stuff & API).

但是如果你使用框架来管理你的 API 会更容易。我建议您使用轻量级框架,也许您也可以轻松地将您的 web 应用程序转换为该框架,让一个“应用程序”返回两个不同的“事物”(网络内容和 API)。

Take a look at Laravel, Laravel 4 based REST APIor a list of popular php rest api frameworksthat can be used to build one.

看看Laravel基于 Laravel 4 的 REST API或可用于构建一个的流行php rest api 框架列表。

回答by Dan Simon

You certainly don't needany kind of framework to build a PHP REST API. REST is nothing more than a protocol convention built on top of HTTP. Since PHP can obviously handle HTTP requests, it has everything you need to build RESTful API's.

您当然不需要任何类型的框架来构建 PHP REST API。REST 只不过是建立在 HTTP 之上的协议约定。由于 PHP 显然可以处理 HTTP 请求,因此它拥有构建 RESTful API 所需的一切。

The whole point of frameworks is to handle common tasks and things that are otherwise tedious. REST API's are commonly built with PHP, so a plethora of frameworks exist. Personally, I would use a lightweight framework like slimsimply to handle things like URI routing, parsing/cleaning request data, and generating responses.

框架的全部意义在于处理常见的任务和其他乏味的事情。REST API 通常使用 PHP 构建,因此存在大量框架。就我个人而言,我会使用像slim这样的轻量级框架来处理诸如 URI 路由、解析/清理请求数据和生成响应之类的事情。

回答by kingmaple

No you do not need a framework to build a REST API, but it is highly recommended, as a well built framework can take care of things that can be very difficult and complicated otherwise, namely session authentications and caching and well separated architecture. Reinventing the wheel only gets you so far.

不,您不需要框架来构建 REST API,但强烈建议使用,因为构建良好的框架可以处理非常困难和复杂的事情,即会话身份验证和缓存以及分离良好的架构。重新发明轮子只会让你走到这一步。

I'm a developer of Wave Frameworkwhich was developed keeping in mind an API-centric design (read more here). I encourage you to take a look into this framework and see if it might be something that could help you. It has a small group of developers, but it is slowly gaining recognition.

我是Wave Framework的开发人员,该框架的开发牢记以 API 为中心的设计(在此处阅读更多内容)。我鼓励你研究一下这个框架,看看它是否可以帮助你。它有一小部分开发人员,但正在慢慢获得认可。

I encourage you to take a look at that and if it might fill your needs.

我鼓励你看看它,如果它可以满足你的需求。

回答by Alexis Panagiotopoulos

There are also tools that create a REST api from the DB without the need for extra code.

还有一些工具可以从数据库创建 REST api,而无需额外的代码。

If you are using Postgres there is the excellent program postgRESTthat

如果您使用的是Postgres有优秀的程序postgREST

serves a fully RESTful API from any existing PostgreSQL database. It provides a cleaner, more standards-compliant, faster API than you are likely to write from scratch.

从任何现有的 PostgreSQL 数据库提供完全 RESTful API。它提供了比您可能从头开始编写的更干净、更符合标准、更快的 API。

回答by kk4You

NO, YOU DON'T NEED ANY FRAMEWORK FOR PHP BACKEND.

不,您不需要任何 PHP 后端框架。

If you are using php as backend then you don't need to use any resetAPI framework. Just create you own php files and generate JSON output for each response.

如果您使用 php 作为后端,那么您不需要使用任何 resetAPI 框架。只需创建您自己的 php 文件并为每个响应生成 JSON 输出。

You must generate JSON output. That is enough. Benefits:- If you use own login to generate Json output then it will be tremendous light speed (based on your logic). If you use any framework then the performance issues will occure.

您必须生成 JSON 输出。足够了。好处:- 如果您使用自己的登录名来生成 Json 输出,那么它将是惊人的光速(基于您的逻辑)。如果您使用任何框架,则会出现性能问题。

It will be better if you can design your own mini framework to do specific site's work.

如果您可以设计自己的迷你框架来完成特定站点的工作,那就更好了。

Thanks

谢谢

回答by Himanshu Vaishnav

REST is more of a design ideology than a language framework, so NO you don't NEED to use any framework. However there is no advantage in reinventing the wheel (Sure some disadvantages are there like security, structure etc.).

REST 与其说是语言框架,不如说是一种设计理念,因此您不需要使用任何框架。然而,重新发明轮子没有优势(当然存在一些缺点,如安全性、结构等)。

If you want to avoid mayhem of MVC (which is not required specification of a REST architecture) you can use any PHP mico-framework (Slim, Lumen etc) they are really quick to learn and implement and allow PHP developer to write route based app (similar to those of MEAN and Express) saving a lot in time.

如果您想避免 MVC 的混乱(这不是 REST 架构的必需规范),您可以使用任何 PHP 微框架(Slim、Lumen 等),它们非常易于学习和实现,并允许 PHP 开发人员编写基于路由的应用程序(类似于 MEAN 和 Express 的那些)节省了很多时间。

Most of these frameworks comes with an MVC as well but if you don't want to give a CMS for the API, MR (Model-Routes) is good enough (and practically the best) for all REST needs.

大多数这些框架也带有 MVC,但是如果您不想为 API 提供 CMS,MR(模型路由)对于所有 REST 需求来说已经足够好(实际上是最好的)。

Since the dawn of angular like frameworks which allows your website to communicate with sever like any other API easily, I think even the CMS should access the API like any other app just with elevated rights or specific end-poin

自从允许您的网站像任何其他 API 一样轻松地与服务器通信的 angular 类框架出现以来,我认为即使是 CMS 也应该像任何其他应用程序一样访问 API,只是具有提升的权限或特定的端点

回答by Deka

The World is very very big, so, no one can do all things alone. Someone will help someone (who do something new) do something old. This is reason, libray and FW exist in our IT World.

世界很大很大,所以,没有人可以一个人做所有的事情。有人会帮助某人(做新事)做旧事。这就是我们的 IT 世界中存在 libray 和 FW 的原因。

In production/live environment, anything is very more complex than we think. Until a day in future, we will need to build so many thing for our project, and we will see that those things has been built by FW completely before.

在生产/现场环境中,任何事情都比我们想象的要复杂得多。直到未来一天,我们将需要为我们的项目构建这么多东西,我们会看到这些东西之前完全是由 FW 构建的。

Although just a RESTful Server, with pure PHP, we need resolve problems with: URI routing, parsing/cleaning request data, data access, dependency management, generating responses, bla bla bla...

虽然只是一个 RESTful 服务器,使用纯 PHP,但我们需要解决以下问题:URI 路由、解析/清理请求数据、数据访问、依赖管理、生成响应、bla bla bla...

I recommend using Slim or Phalcon (Micro App). Slim is a easy and quickly method, but Phalcon is a effective and high performing method.

我推荐使用 Slim 或 Phalcon (Micro App)。Slim 是一种简单快捷的方法,但 Phalcon 是一种有效且高性能的方法。

回答by Mahmoud Zalt

Not necessarily, you don't really needa framework for anything! The framework is made just to provide you with tools to help building your projects faster. These days in most cases it is smarter to use a framework, cause who likes to reinvent the wheel!

不一定,您实际上不需要任何框架!该框架旨在为您提供帮助更快构建项目的工具。这些天在大多数情况下使用框架更聪明,因为谁喜欢重新发明轮子!

Since you are a PHP developer and the best answer suggests to take a look Laravel, I would also suggest that you to take a look at Apiato(apiato.io), this is a PHP API frameworkbuilt on top of Laravel. It lets you create your API way faster than starting with a vanilla Laravel, as it provides you with tens of features that you already need for every API.

由于您是一名 PHP 开发人员,最佳答案建议您查看Laravel,我还建议您查看Apiato( apiato.io),这是一个构建在 Laravel 之上的PHP API 框架。它让您可以比从普通 Laravel 开始更快地创建 API,因为它为您提供了每个 API 已经需要的数十种功能。