Slim - change settings in middleware - slim

Am I able to change settings in middleware?
What I want to accomplish is to change error handling engine depending on route.
For some routes I want error to be displayed in HTML and for some (ajax) as JSON.
I think that middleware is the right place but how to change settings?

You are able to change error handler by overwriting the error handler key in your container:
$app->add(function($req, $res, $next){
//$this is an instance of Slim\Container
$this['errorHandler'] = new SomeErrorHandler();
return $next($req, $res);
});

Related

"Client Error" message when trying to implement an Rest API View

I'm creating an API with Drupal to serve a angular application. The problem is, when I create the view with Rest export and try to access it (via browser or postman), it gaves me an "Client error" message (and nothing more).
I'm using drupal-8.7.6, running with PHP7.3, mysql and apache 2. But I've tried at nginx also and the error appeared again.
To reproduce it, just add some contents, enable the Restful web services, jsonm hal and create a view that exports rest data.
I expect to access the information via GET request (using browser, postman or any other way)
Go to your View
Go to Format
Click on Settings beside Serializer
Finally check Json option and save.
You are done :)
find out what was happening
I was not specificating the format of output, so Drupal was searching for an html to serve. To workaround this, just add to url: ?_format=json, for example, if my view route is "/articles", it will be: http://drupal.dev/articles?_format=json
Go to the rest exports view, click on format setting and click accepted request format to json.

How do I know I am being called from a progresive web app?

I've been following the guidance on google to create a PWA, but I am interested if there are any conventions to communicate with your data server that you are being called by a PWA. Is the easiest thing to add a parameter to the request i.e.
var dataUrl = 'https://query.myapi.com/v1/get?source=pwa';
and then check the source parameter? Or should I add to the request header?
In your code
if (window.matchMedia('(display-mode: standalone)').matches) {
// do things here
// set a variable to be used when calling something
// e.g. call Google Analytics to track standalone use
}
** Assumption made that you are also setting up your app to be added to the users home screen as a shortcut
You have to pass in the traffic source to Google Analytics indicating its from PWA. Refer this answer on more details.

Using mashape api in sapui5

I'm fairly new to sapui5 and I'm trying to use the Hearthstone API in order to show a list of cards.
I've registered in the page and I've obtained the X-Mashape-Key which I'm using to try to connect to the API.
For this, I've created a destination with the following information:
Besides this, I've created a view and its corresponding controller with the onInit function, with the following code:
Being the sUrl: /hsApi/cards and oHeaders:
var oHeaders = {
"X-Mashape-Key": "key"
};
The result that I obtain is the following one:
Request is failing stating that I'm not authorized to request the information, even though that doing the same request in PostMan I'm obtaining the information.
Most likely, this is caused by something really obvious but I'm not able to see it.
Could you try adding the following property to your destination? I am not sure what this property does exactly, but maybe you need it to allow this destination to be used in your WebIDE. Also try changing your "True" and "TRUE" to lowercase "true".
But I think you don't need any "Additional property" at all. Can't hurt to try it without them aswell.
Please keep in mind that you need to restart your WebIDE everytime you change something in your destinations as those are loaded statically every time the WebIDE loaded.

ZF2 main router in console

How can I use the url() controller helper inside the console?
I have to generate some urls using an action of the console, but if I call $this->controller->url()->fromRoute(...);, I can only ask for routes defined in the console router.
In other words, I need the ability to call all the routes of the application's main router.
Thanks
I'm not sure, that it is a good decision, but you can change router manually:
// ConsoleController.php
// Change router to HTTP
$this->getEvent()->setRouter($this->getServiceLocator()->get('HttpRouter'));
// Get any HTTP route
var_dump($this->url()->fromRoute('your_http_route'));
// Change it back, if you want
$this->getEvent()->setRouter($this->getServiceLocator()->get('Router'));

Play 2.1.x default catch all route

Is there a default catch all fallback route in Play framework? If so how can I configure it in my routes file? Currently, there are some urls that I do not want the user to call them and even if they call, I don't want that error page appearing, rather I want them to go to the landing page of my web app! Is there a way to do that in the routes configuration file?
Simply define a route matching any path at the end of your routes file. Do not forget to define specific routes for your assets though, for example:
GET / controllers.Application.index
GET /some/path controllers.Application.someHandler
...
# End of file
GET /favicon.ico controllers.Assets.at(path="/public", file="img/favicon.ico")
GET /$file<(css|img|js|partials)/.*> controllers.Assets.at(path="/public", file)
GET /$path<.*> controllers.Application.catchall(path)
Any URL not matched by an earlier rule will be matched by this one.
Catch all routes makes sense when you want to do something with the path (ie. resolve it manually in your custom action), otherwise it's enough to use common onHandlerNotFound in your Global object and redirect request wherever you want.