How do I create my own API in Yii2? - api

I want to create API methods which you can access form another platform. Just see the following diagram.
In the above image you can see how to fetch data from Yii2 app using another application, I wanna use same procedure when I going to push data into Yii2 application from the another application. That's means Yii2 just provides api methods to clint to push/pull data.
Give me some suggestion?

Yii2 provides ready to use RESTful Web Service API. http://www.yiiframework.com/doc-2.0/guide-rest-quick-start.html

First of all, you will need separate module for API.
If you use Advanced template (with backend, frontend), you'll need to create api directory in root level of your project. If your project is big, and API will changes in future and your API is for many clients, you should make versioning API. Create directory "modules" in api directory, then create directory "v1" in "modules". In future, if you'll need make a huge changes, create second directory, like "v2".
If you use Basic template, just create modules directory, then create API module.

Related

PrestaShop - calling module methods from Vue.js back office application

I'm working on PrestaShop module. I have Vue.js app which is running in the backoffice, and I want to make an http request to my module class in order to populate some data. How can this be achieved?
I want to be able to simply expose some kind of API, or call php method in some way.
I was able to expose some API endpoints to FrontEnd of module using custom fronts controllers, but I have no idea how to make this work in backoffice. Any help would highly valuable for me, thanks!
Like for the Front office, you can create controllers inside the Back office. Here's a page from the developer documentation:
https://devdocs.prestashop.com/1.7/modules/concepts/controllers/admin-controllers/
Make sure to check some of the examples from the native modules:
https://github.com/PrestaShop/blockwishlist/blob/dev/src/Controller/WishlistConfigurationAdminController.php
https://github.com/PrestaShop/example-modules/tree/master/democontrollertabs

How can I set Vue environment variables without them being included in the JS files after 'build'?

I'm building a Vue app that works as a SPA that gets rendered by my Express backend.
In this Vue app I'm using environment variables. One of these variables is an API key to an external API service.
Obviously I don't want to show the whole world my API key, however after I run the build command it includes all the env variables in the Javascript files, which I believe makes it accessible to anyone.
What would be the right way to go here? Should I make a route in my Express backend that handels requests to this external API, so that I can safely store my API key on the server side?
Or is there a way to make my Vue app autonomously send requests to the external API without having to show the API key?
I'm using Vue 2 by the way.
Yes you should make a backend that will interact with API using your key instead of the front end doing that no matter which version of VueJs your are using
VueJS is front end javascript, which mean users can see the code even minified on their browser
is what you are looking for
Your could use Nuxt, which is an SSR solution to build your app before shipping it to production. During the build, you can get use some sensible stuff since it will run NodeJS and will be on the server only. That way, you could for example fetch some variables and let's say get the content of some blog articles. Then, it will be available on the frontend with the end result only and all your variables safe since they will be used only on the server build.
Other solution is to use your backend as a middleware to do exactly the same process of using something sensitive and just fetch the end result.
Also, serverless functions like on Firebase or Azure are great + cheap for this purpose.

Cumulocity File Repository

I am trying to load a csv file in the Cumulocity -> Administration - > Management - > File Repository. My objective is to access the file contents in the HTML widget and show the data in tabular or chart format.
I tried using the fetch() method to use csv file url , but it was not able to fetch the file. The file I believe has been locally stored into the Cumulocity platform now. How can I access the file via fetch().
You are trying to abuse the HTML widget for something it wasn't designed for.
Instead of hacking such logic into the HTML widget you should just extend the Cockpit application with your own widget which is honestly not much more difficult using the WebSDK. https://cumulocity.com/guides/web/introduction/
There is a XSRF protection to prevent exactly what you are currently trying: Bringing in custom javascript code into the live application and trying to access the platform API by "hijacking" the cookies of the logged in user (and you would either need the cookies or hardcode some credentials as you cannot access the API unauthenticated).
If you use the SDK there is even a proper Angular service in order to fetch the file for the API.

Add Function to Existing Angular Controller in VirtoCommerce Storefront

I would like to add a function to the cartController (angular) in the storefront.
I could fork the storefront and then make my change and start loading only "my" storefront.
Is there a way to build my own module with the purpose of extending or overriding the controller so I can still use the base storefront module and just extend with my changes.
To extending storefront you should adhere to the following rules to be able to update to the latest storefront version without 'merging hell':
Try to avoid direct exist storefront controller changes, create own
controllers in separated solution folders with using prefixes for class and file names. e.g
VirtoCommerce.Storefront/MyExtension/myCartController
Register new routes and dependencies by modifying Startup.cs (it
is exception)
All storefront model classes marked as partial and will be easily
to split the definition over two or more source files within
VirtoCommerce.Model and VirtoCommerce.LiquidThemeEngine projects. e.g
VirtoCommerce.Storefront.Model/MyExtension/ShoppingCart.cs
About theme customization please read my answer on Starting VirtoCommerce Storefront Theme Development stackoverflow question
How to use new modules API in the storefront described in this article How to generate module API C# client using AutoRest
In this article you can see the overall solution development process.

MVC4 Web API - OK to delete all views/javascript?

Just created a new MVC4 Web API project - however I don't want any client-side for this, just the back-end to be referenced from other clients.
Is there any, sensible, reason that the default project template includes all of the javascript, views, etc?
Can they be removed without issue?
Default the web api project also contains the regular mvc stuff. If you do not intent to use the web api help pages (https://www.nuget.org/packages/Microsoft.AspNet.WebApi.HelpPage) or intend to develop a website part next to your api you can easily remove all views, scripts & statics.