using the $app object inside a controller (Lumen) - lumen

Ok im farly new to lumen and laravel and im trying to make this api https://packagist.org/packages/codenexus/lumen-geoip work in lumen, i installed it via composer and im able to use it and obtain locations based on the IP given when im inside the routes.php
But when i'm using the same code inside a controller its doesnt let me proceed cause the $app variable is not defined.
This is the code that works inside the routes
$app->geoip->getLocation('148.210.21.180')->country->names['en']
I have been reading that the service container could help me in this solution but i feel lost...
Thanks in advice for the help.

The $app variable is not available inside your controllers, but you may use the app() method anywhere in your code to access the application object.
So, try this in your controller:
app()->geoip->getLocation('148.210.21.180')->country->names['en']

Related

Getting started with a custom function with Excel Add-Ins

I followed this guide: https://learn.microsoft.com/en-us/office/dev/add-ins/quickstarts/excel-custom-functions-quickstart?tabs=excel-windows
So far everything went fine. I can use the =CONTOSO.CLOCK and other example functions.
If I add a function to the functions.ts file, it rebuilds... but I can't use the custom function on the web in any way.
Somehow I'm missing how I can register this function and run it in Excel...
Other question is: How can I change the name of the namespace from CONTOSO in something else?
Regards, Peter
Okay, a valid JSDoc was needed to work. After running a new build it worked out. https://learn.microsoft.com/en-us/office/dev/add-ins/excel/custom-functions-json-autogeneration

Why is my dependency container variable undefined when I try to register csrf middleware for all routes in Slim?

I am trying to register middleware for all routes to protect against csrf in Slim. I have installed the files for Slim csrf protection using composer and I have added the dependency to my dependencies file, but when I try to register the middleware in my middleware file, I get an error telling me that my dependency container variable is undefined.
I am a programming newbie and do not understand how to troubleshoot things within a framework. So I have just tried fiddling with it.
Dependency in dependencies.php:
$container['csrf'] = function ($c) {
return new \Slim\Csrf\Guard;
};
Register middleware in middleware.php:
use Slim\App;
return function (App $app) {
$app->add($container['csrf']);
};
It should work, especially considering I literally did exactly what the documentation says to do in order to set this up, but when I refresh my page after I get a notice message telling me $container is not defined. Because I am completely new to using a framework I could really use some insight. Thanks.
The App is not the container itself; it holds the container, so you need to get at it first.
Try:
return function (App $app) {
$app->add($app->getContainer()->get('csrf'));
};
Note that I've also used the get() method on the container for retrieval. This is the method that PSR-11 uses and so will work with a variety of containers. Using array access to retrieve items from the container is Pimple specific.

How to access SAPUI5 component from utility functions?

I would like to access my SAPUI5 app component using this.getOwnerComponent() from a utility function, for example formatter or a class function, this works from controllers but in a utility or formatter function placed in another folder doesn't work. I do not want to use sap.ui.getCore(), is there any other way to do it?
I ended up with below two choices:
1. Create the utility functiona as a singleton class and pass the component one time with a setComponent method.
2. Use event bus to fire events and get things done by component that can be done only within the component like accessing oData resources
You could pass the controller instance to your utility class in the constructor, and then access its owner component. You could also try injecting the component directly. Unfortunately, I am not sure if any of these is the preferred way in UI5 development.
That would couple the utility function to the component logic though, which I do not think is a good idea, you probably should pass only what is required for the utility function to do its' job.

Trying to use pubnub with lumen

Im trying to use pubnub with lumen, and failing spectacularly.
Its in the vendor folder under pubnub/pubnub/composer/lib/autoloader.php
Or pubnub/pubnub/composer/lib/Pubnub/Pubnub.php.
How exactly do I access this??
Ive tried puttint it in bootstrap/app.php like so...
require_once __DIR__.'/../vendor/pubnub/pubnub/composer/lib/Pubnub/Pubnub.php';
and...
require_once __DIR__.'/../vendor/pubnub/pubnub/composer/lib/autoloader.php';
That didnt work. I've tried doing that in the actual controller file that I need it in. That didnt work either because of namespacing. I feel like I'm being really stupid right now, but how the hell do I just access this class!?
PS: I don't want to use Broadcasting built into Lumen. As this is just a one off route, that doesnt need all that complexity. I simply just want to fire off a Pubnub simply by loading the class, and doing it.

Migrate custom Facebook util library to Yii framework

I have a facebook app developed in plain PHP, I'm migrating the app to YII framework.
The thing is that I use a class call "utilsFacebook" where I have the object facebook(of the fb sdk) and all the methods that I need to get data from facebook, getUserId, getUserFriendList, etc.
I don't know how to handle all the operations that I do in utilsFacebook with Yii.
Create a controller with the functions of utilsFacebook is the correct think to do?
Every time that I instance the controller would create a new Facebook object, Should I store that object in a SESSION to get a better performance or is a bad idea?
Q. Create a controller with the functions of utilsFacebook is the correct think to do?
Having done a facebook app using yii as the framework, i would recommend you to make this library either a component, or an extension.
But definitely don't put these functions in the controller directly. Whenever a controller needs them call the functions using your custom facebook util class.
Components can be put in the folder: projectrootfolder/protected/components
Extensions can be put in the folder: projectrootfolder/protected/extensions
If you don't believe that either of these make semantic sense, you can always create a new folder within protected, say utils and put the class there. However i think extensions is the best way to go.
Q. Should I store that object in a SESSION to get a better performance or is a bad idea?
I don't think it's necessary to store the object in a session, because there will be no visible performance gain. Further you'll complicate your code unnecessarily.
What i had done was, created an app level component and used this component throughout the app, in any controller.
Example:
In your application's config, protected/config/main.php :
'components'=>array(
'fbHelper'=>array( // gave the component this name
'class'=>'ext.utils.FacebookHelper', // had stored the helper class in extensions/utils folder
'parameter1'='somevalue',
// more parameters
),
// standard yii app components
),
This will allow you to use the component like this: Yii::app()->fbHelper->getFriends();
Take a look at the facebook-opengraph extension, which could help you, on the way.