Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why create a separate application for RESTful API?

Tags:

rest

api

yii2

In the guide for Yii 2 it is said:

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.

Source: RESTful Web Services - Quick Start

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?

like image 493
robsch Avatar asked Oct 07 '14 07:10

robsch


People also ask

Why should we use the REST API in your application?

One of the key advantages of REST APIs is that they provide a great deal of flexibility. Data is not tied to resources or methods, so REST can handle multiple types of calls, return different data formats and even change structurally with the correct implementation of hypermedia.

Can an application have both SOAP and REST?

A soaprest endpoint is specified as a SOAP web service using WSDL, but will also accept REST invocations, and will respond according to how a request was invoked. This gives one the advantages of both SOAP and REST in a single web service.

What are two advantages of using the REST API choose two?

The advantages of REST for development For example, it improves the portability of the interface to other types of platforms, it increases the scalability of the projects, and allows the different components of the developments to be evolved independently.

What makes an application RESTful?

In order for an API to be considered RESTful, it has to conform to these criteria: A client-server architecture made up of clients, servers, and resources, with requests managed through HTTP.


2 Answers

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

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

Here is good example for RESTful API with advance template

Setup RESTful API in Yii2(budiirawan)

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'],

for authentication basically we can use 'HTTP Basic Authentication'

like image 70
Chanuka Asanka Avatar answered Oct 09 '22 14:10

Chanuka Asanka


This article explain the idea and the why , also it provide you a starter project called "yii2-advanced-api": http://budiirawan.com/setup-restful-api-yii2/

like image 20
osmancode Avatar answered Oct 09 '22 14:10

osmancode