Why create a standalone application for the RESTful API?

The manual for Yii 2 says:

While not required, it is recommended that you develop your RESTful API as a standalone application, distinct from your web interface and back to facilitate maintenance.

Source: RESTful Web Services - Quick Start

What does it mean? Will it be a completely different application or may it be in the same folder as the "regular" web application? I just started with my application so that I can easily change things, more or less. But I wonder: if I create another application, then my business logic will not be available.

Why and how should I create another application? And when is this not required?

+7
rest api yii2
source share
3 answers

This means that you need to create an application such as frontend or backend ( Yii 2 extended application template ), what you need to do is create another api directory call, the same as backend or frontend, and it will contain the folder structure same as backend | frontend, except for assets, views, widgets, etc.

Basically you need a folder structure like this

api -config -modules --v1 ---controllers ---models -runtime -tests -web backend common console environments frontend 

If you are going to use the Yii 2 basic application template to create a rest api, this is possible. create an api module call and create a v1 subdirectory as a submodule. (The Yii doc -A module may consist of submodules.) (GiovanniDerks - backend sub-modules)

 -modules --api ---v1 ----controllers ----models 

There is an advantage to using one of these folder structures because you don’t have to worry much about the route.

 https://domain.com/api/v1/products 

Here is a good example for a RESTful API with a preliminary template

Install RESTful API in Yii2 (budiirawan)

API and RESTFull API are different. The RESTFull API must have REST standards. mainly why APIs are developed as a standalone application. in a regular application, we create 4 actions for CRUD functions. but in the yii2 RESTFull API we just create one action for all CRUD functions. (Controllers expand from the active REST controller - yii \ rest \ ActiveController). in the kernel code you can find 4 actions for different headers GET, POST, PUT and DELETE.

 'index' => ['GET', 'HEAD'], 'view' => ['GET', 'HEAD'], 'create' => ['POST'], 'update' => ['PUT', 'PATCH'], 'delete' => ['DELETE'], 

for authentication we can mainly use "HTTP Basic Authentication"

+13
source share

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

+1
source share

IMHO if you need a REST API for Angular.js or Knockout.js. AJAX incurs overhead on your site to do this as a standalone application. Because you will have problems with cross-domain AJAX calls (especially for POST requests).

I think it’s enough to make a module (API) in the interface for the REST API

+1
source share

All Articles