为什么要为RESTful API创建单独的应用程序? [英] Why create a separate application for RESTful API?
问题描述
Yii 2指南中说:
In the guide for Yii 2 it is said:
虽然不是必需的,但建议您开发RESTful API是独立的应用程序,不同于您的Web前端和 后端,易于维护.
While not required, it is recommended that you develop your RESTful APIs as a separate application, different from your Web front end and back end for easier maintenance.
这是什么意思?这将是一个完全不同的应用程序,还是与普通" Web应用程序位于同一文件夹中?我刚刚开始使用我的应用程序,因此我可以轻松地或多或少地进行更改.但我想知道:如果我要创建另一个应用程序,将无法访问我的业务逻辑.
What does this mean? Would this be a completely different application or can it be in the same folder as the 'normal' web application? I've just started with my application so I can change things easily, more or less. But I'm wondering: if I would create another application than my business logic would not be accessible.
为什么以及如何创建另一个应用程序?何时不需要?
Why and how I should create another application? And when it's not required?
推荐答案
这意味着您必须创建前端或后端之类的应用程序( Yii 2高级应用程序模板), 您需要做的是创建另一个与后端或前端相同的目录"api",它包含与后端|前端相同的文件夹结构,除了资产,视图,小部件等.
It means you have to create an application like frontend or backend(Yii 2 advanced application template), what you have to do is create another directory call 'api' same as backend or frontend, and it'll contain folder structure same as backend|frontend except assets, views, widgets etc.
基本上,您需要像这样的文件夹结构
Basically you need folder structure like this
api
-config
-modules
--v1
---controllers
---models
-runtime
-tests
-web
backend
common
console
environments
frontend
如果您要使用 Yii 2基本应用程序模板来开发rest api,则可以.创建模块调用"api"并创建子目录调用"v1"作为子模块. (Yii文档-一个模块可能包含子模块.) (GiovanniDerks-后端子模块)
If you'r going to use Yii 2 basic application template to develop rest api, it's posible. create module call 'api' and create a sub directory call 'v1' as sub-module. (Yii doc -A module may consist of sub-modules.)(GiovanniDerks - backend sub-modules)
-modules
--api
---v1
----controllers
----models
使用这些文件夹结构中的一种是一个优点,因为您不必担心路由太多.
There is an advantage of using one of these folder structure, because you don't have to worry about route much.
https://domain.com/api/v1/products
这是带有高级模板的RESTful API的好例子
Here is good example for RESTful API with advance template
在Yii2(budiirawan)中设置RESTful API
API和RESTFull API是不同的. RESTFull API必须具有REST标准.基本上,这就是为什么API作为单独的应用程序开发的原因.在普通应用中,我们为CRUD函数创建4个动作.但是在yii2 RESTFull API中,我们只为所有CRUD函数创建了一个动作. (控制器从REST Active Controller扩展-yii \ rest \ ActiveController).在核心代码中,您可以找到针对不同标头GET,POST,PUT&的4个操作.删除.
API & RESTFull API are different. RESTFull APIs have to have REST standards. basically that's why APIs are developed as separate application. in normal app, we create 4 actions for CRUD functions. but in yii2 RESTFull API we just create One action for all CRUD functions. (Controllers extend from REST Active Controller - yii\rest\ActiveController ). in core code you can find find 4 actions for different headers GET,POST,PUT & DELETE .
'index' => ['GET', 'HEAD'],
'view' => ['GET', 'HEAD'],
'create' => ['POST'],
'update' => ['PUT', 'PATCH'],
'delete' => ['DELETE'],
对于身份验证,基本上我们可以使用"HTTP基本身份验证"
for authentication basically we can use 'HTTP Basic Authentication'
这篇关于为什么要为RESTful API创建单独的应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!