Yii2 - REST + CRUD - best practices implementation - rest

As far as I understood, unlike Rails, Yii2 still doesn't use RESTful routing for CRUDs.
What could be the most efficient way to generate new CRUD with gii and add REST routing support to it?
I've done some background search and found the following:
a lot of information about how to easily build REST API application - http://www.yiiframework.com/doc-2.0/guide-rest-quick-start.html (which is not a thing I really need - it's only useful for backend applications)
an article by qiangxue https://github.com/yiisoft/yii2/blob/master/docs/guide/rest-routing.md on how to enable REST routing for CRUDs
a gist added by cebe (https://gist.github.com/cebe/5674918) doing almost the same thing like qiangxue proposed in other way
So it's all about configuring UrlManager and implemetation of backend API applications.
Unfortunately, I also need to somehow add a REST routing support to CRUD controller generated by default (with its verb-based filters and a base class that is not specialized for REST, unlike yii\rest\ActiveController for example). At the same time a generated by default GridView widget code doesn't support REST routes, but only utilizes GET+POST verbs.
Really appreciate certain list of things to do :)

Related

Is there any tool to automatically create wiki-like REST API documentation from JSON or Open API?

I have several services, each one exposed through REST API with ASP.NET Core Web API. I use Swashbuckle for ASP.NET Core tooling in order to automatically generate from my controllers and DTOs all the necessary documentation and visualize it in SwaggerUI. I found this tooling really great, with little annotations on my models and my controllers already provides many features out of the box, such as a UI client to try out the REST API endpoints.
But with this solution each service has its own dedicated SwaggerUI instance and therefore UI.
I would like to offer to my customers a wiki-like documentation with a navigation menu, where, for instance, they can browse sections regarding all the endpoints exposed by my services and have on each page the same features offered by SwaggerUI.
It can be achieved by creating my own web application but I was wondering whether an out of the box solution or some tool that might ease such integration already exists.
I tried Slate but I felt like I had to re-invent the wheel in order to automate at least the creation of the basic API documentation, namely controller definition, response definition and descriptions. Does anyone have any suggestion?
I faced this very issue recently working in a microservices architecture, you're absolutely right. There is not need to reinvent the wheel.
I really can't recommend redoc by Redocly enough in this case.
Have a look at the multiple-apis example.

Is it a good choice to adapt rest api to meet exact front web client use cases?

I use Angular2 with django rest framework on back.
Is it a good practice to consider designing a rest API that meets exactly front implementation use cases. routes, which fields to include... ? This makes front project far more maintainable than interacting with a general API that gives all data access possibilities without caring about font needs (some overhead include: unused fields, double front models for UI + for API).

Constructing a back-end suitable for app and web interface

Let's suppose I was going to design a platform like Airbnb. They have a website as well as native apps on various mobile platforms.
I've been researching app design, and from what I've gathered, the most effective way to do this is to build an API for the back-end, like a REST API using something like node.js, and SQL or mongoDB. The font-end would then be developed natively on each platform which makes calls to the API endpoints to display and update data. This design sounds like it works great for mobile development, but what would be the best way to construct a website that uses the same API?
There are three approaches I can think of:
Use something completely client-side like AangularJS to create a single-page application front end which ties directly into the REST API back-end. This seems OK, but I don't really like the idea of a single-page application and would prefer a more traditional approach
Create a normal web application (in PHP, python, node.js, etc), but rather than tying the data to a typical back end like mySQL, it would basically act as an interface to the REST API. For example when you visit www.example.com/video/3 the server would then call the corresponding REST endpoint (ie api.example.com/video/3/show) and render the HTML for the user. This seems like kind of a messy approach, especially since most web frameworks are designed to work with a SQL backend.
Tie the web interface in directly in with the REST api. For example, The endpoint example.com/video/3/show can return both html or json depending on the HTTP headers. The advantage is that you can share most of your code, however the code would become more complex and you can't decouple your web interface from the API.
What is the best approach for this situation? Do you choose to completely decouple the web application from the REST API? If so, how do you elegantly interface between the two? Or do you choose to merge the REST API and web interface into one code base?
It's a usually a prefered way but one should have a good command of SPA.
Adds a redundant layer from performance perspective. You will basically make twice more requests all the time.
This might work with super simple UI, when it's just a matter of serializing your REST API result into different formats but I believe you want rich UI and going this way will be a nightmare from both implementation and maintainance perspective.
SUGGESTED SOLUTION:
Extract your core logic. Put it into a separate project/assembly and reuse it both in your REST API and UI. This way you will be able to reuse the business logic which is the same both for UI and REST API and keep the representation stuff separately which is different for UI and REST API.
Hope it helps!
Both the first and the second option seem reasonable to me, in the sense that there are certain advantages in decoupling the backend API from the clients (including your web site). For example, you could have dedicated teams per each project, if there's a bug on the web/api you'd only have to release that project, and not both.
Say you're going public with your API. If you're releasing a version that breaks backwards compatibility, with a decoupled web app you'd be able to detect that earlier (say staging environment, given you're developing both in-house). However, if they were tightly coupled they'd probably work just fine, and you'll find out you've broken the other clients only once you release in production.
I would say the first option is preferable one as a generic approach. SPA first load delay problem can be resolved with server side rendering technique.
For second option you will have to face scalability, cpu performance, user session(not on rest api of course because should be stateless), caching issues both on your rest api services and normal website node instances (maybe caching not in all the cases). In most of the cases this intermediate backend layer is just unnecessary, there is not any technical limitation for doing all the stuff in the recent versions of browsers.
The third option violates the separation of concerns, in your case presentational from data models/bussines logic.

OpenUI5 with Java Services

I am exploring OpenUI5 to use it as front end for developing SAP applications. One thing I noticed is that it is more user friendly with OData services/OData model. I believe, it may be a close initiative of Microsoft with SAP or vice-versa.
I am using Java technology along with JCo outside SAP environment. I am exploring for the best options.
Should I expose my Java services as OData services to leverage OpenUI5 capabilities?
1 I see that Java implementations for OData support is limited to Version 2 and Version 4 is underway. This community is very small, not surprisingly.
Should I limit the usage of OpenUI5 only for: application definitions, view definitions, layouts, etc. and use Java services (e.g. REST/SOAP)?
1 Here, I have to add my JavaScript/jQuery logic for the CRUD operations on UI controls. I am not sure how much of it is supported for plain JSON/XML objects if I push JSON/XML response from server.
Just to be clear, I hope you have a clear understanding of Odata and REST. The difference is that OData is a specific protocol; REST is architecture style and design pattern.
The OData protocol is built on top of the AtomPub protocol. The AtomPub protocol is one of the best examples of REST API design. So, in a sense you are right - the OData is just another REST API and each OData implementation is a REST-ful web service.
Odata is the way to go since with Odata Model & binding, the pain of writing a lot of javascript is reduced by a huge proportion.
I think you should keep it simple and use REST API and Json to communicate between your back end and SAPUI5 front end.

Ember.js & REST API

From all the various examples of Ember.js, I have not been able to figure out if there is a default method in Ember.js to do REST AJAX calls. Many examples build their own interfaces for CRUD operations. I even tried to sift through the code to find any reference to AJAX calls but came up with nothing.
So, my question is, is there a default implementation of REST API in Ember.js. If yes, how do I use it? Also if, for a specific application, I want to build custom CRUD methods, where do I plug these into Ember.js?
It seems that Ember Data is what you are looking for. It is part of emberjs organiztion in GitHub.
[2014-02-18: Deprecated - I no longer support ember-rest because it is overly simplistic, and would recommend using ember-data for most ember projects. Check out the Ember guides for an overview of ember-data as well as this example project ]
While learning Ember, I decided to create a very simple Ember REST library. I also wrote an example Rails CRUD app.
My goals were to keep this project as simple as possible, while still including error handling and validation. Ember REST is certainly much leaner than Ember Data and Ember Resource, and I hope you'll find the code well commented and accessible.
There is a Ember Resource library aiming REST JSON interfaces. It provides Ember.Resource class with save(), fetch() and destroy() operations that could be easily overriden. Looks like it should be more mature than Ember Data for now.
Ember.js can work nicely with Ember Data. That said, there is a specific format of REST to follow. When followed, you can streamline the process of connecting API with Ember and have so much less work.
In case you use custom REST, the place to adjust is:
adapter - to inform from where you like to get data
serializer - how data should be adjusted for custom REST API