Using an extension's class from inside my controller (yii2 application) - php

I'm trying to use a method from inside my controller.
This works inside my "view"
use aweber\aweber\aweber_api;
$consumerKey = "XXXX";
$consumerSecret = "XXXXX";
$aweber = new AWeberAPI($consumerKey, $consumerSecret);
but it doesn't work in my controller.
I get the error
Class 'app\controllers\AWeberAPI' not found
Any hints?

I think you extracted AWeber-API-PHP-Library to the path aweber\aweber\aweber_api. The directory structure could be
$ ls aweber\aweber\aweber_api
aweber.php
aweber_api.php
....
If you autoloading classes with composer, AWeberAPI is in root namespace
use \AWeberAPI;
$aweber = new AWeberAPI($consumerKey, $consumerSecret);
If you are not autoloading with composer
require_once('aweber_api/aweber_api.php');
$aweber = new AWeberAPI($consumerKey, $consumerSecret);

Normally the extensione related are in vendor namespace ..
try with
use vendor\aweber\aweber_api;

You have a typo. The name of the php file usually is the same name of the class. So your code should be:
use aweber\aweber\AWeberAPI;
If it isn't, you can try:
use aweber\aweber\aweber_api as AWeberAPI;

Thanks for all the comments here. To fix it I actually moved this functionality into my "view" instead of having it in my controller and it worked.
I wasn't able to test the responses here, so if anyone else if facing the same issues, I'd recommend trying some of these answers.

Related

Doctrine ORM not working with the Migrations

Good Day, my friends.
I want to use the doctrine ORM with the Migrations.
The issue is next: I want to place the migration configuration file in the specific folder. For example: 'config/doctrine-migrations.php'.
Everything working fine when I follow the official documentation and place the migrations.php file in the root folder, but when I try to place it in the specific folder system is not working.
My cli-config.php content is:
<?php
require_once "app/bootstrap.php";
return \Doctrine\ORM\Tools\Console\ConsoleRunner::createHelperSet($container->get(\Doctrine\ORM\EntityManager::class));
Well, I can change this file in a next way:
<?php
require_once "app/bootstrap.php";
return \Doctrine\Migrations\DependencyFactory::fromEntityManager(
new \Doctrine\Migrations\Configuration\Migration\PhpFile(BP . '/config/doctrine-migrations.php'),
new \Doctrine\Migrations\Configuration\EntityManager\ExistingEntityManager($container->get(\Doctrine\ORM\EntityManager::class))
);
After this, Doctrine Migration working fine, but Doctrine ORM stop working with the next error:
Argument #1 ($helperSet) must be of type Symfony\Component\Console\Helper\HelperSet, Doctrine\Migrations\DependencyFactory given
If someone knows how to solve my issue and use a specific config file please clarify a possible solution.
Best Regards, Mavis.
I may be a bit late, but i ran into the same problem as you.
You can use the same cli-config for migrations and orm. For that, you need add
the orm commands manually to the cli-config.php.
My cli config looks like this:
$em = getEntityManager();
$config = new PhpFile('migrations.php');
$dependencyFactory = DependencyFactory::fromEntityManager($config, new ExistingEntityManager($em));
$migrationCommands = [
new Command\DumpSchemaCommand($dependencyFactory),
new Command\ExecuteCommand($dependencyFactory),
new Command\GenerateCommand($dependencyFactory),
new Command\LatestCommand($dependencyFactory),
new Command\ListCommand($dependencyFactory),
new Command\MigrateCommand($dependencyFactory),
new Command\RollupCommand($dependencyFactory),
new Command\StatusCommand($dependencyFactory),
new Command\SyncMetadataCommand($dependencyFactory),
new Command\VersionCommand($dependencyFactory),
];
$customCommands = [];
$commands = array_merge($migrationCommands, $customCommands);
ConsoleRunner::run(new SingleManagerProvider($em), $commands);
All my import statements, are not included in this example, but you can
get them from the docrtine documentation
Looks like I found an answer.
I can add custom integration in my application by this guide: https://www.doctrine-project.org/projects/doctrine-migrations/en/3.0/reference/custom-integration.html
In a custom file, I can configure whatever I want.
Hope that this solution will help somebody else.

How to properly add Red Bean PHP into my project

I'm not very experienced with php projects structure, I found this awesome and simple tutorial: https://arjunphp.com/creating-restful-api-slim-framework/ how to create simple slim rest app.
This is actually PHP SLIM's official project structure, my question is what is best and proper way to add and use RedBean php ORM, I dont want on every route to include something like this
use \RedBeanPHP\R as R;
R::setup( 'mysql:host=localhost;dbname=mydatabase', 'myusername', 'mypassword)
and then
$book = R::load( 'book', $id );
And then use ReadBean for my db stuff. Im wondering how to include RedBeans into project and then just use it where i need it. This is my project structure https://github.com/iarjunphp/creating-restful-api-slim-framework3.
Note: i added red beans via composer like its described here https://github.com/gabordemooij/redbean
You can put the code for setting up your libraries in any file that is going to be included on each request, so assuming you're using slim/slim-skeleton, src/dependencies.php is probably the place you want to add these two lines:
use \RedBeanPHP\R as R;
R::setup( 'mysql:host=localhost;dbname=njux_db', 'root', '');
Then you can use ReadBeans in your route callbacks but you also need to add the use \RedBeanPHP\R as R; statement to your src/routes.php as well (or any file that is going to use this class)
If you use a MVC framework (which I recommend) like codeigniter it's pretty easy.
You only have to copy your rb.php to the application/third_party folder.
Then create a file called application/libraries/rb.php containing a code like this one.
<?php
class Rb {
function __construct() {
include(APPPATH.'/config/database.php');
include(APPPATH.'/third_party/rb.php');
$host = $db[$active_group]['hostname'];
$user = $db[$active_group]['username'];
$pass = $db[$active_group]['password'];
$db = $db[$active_group]['database'];
R::setup("mysql:host=$host;dbname=$db", $user, $pass);
}
}
?>
...and vĂ´ila. RedBean will read your database configuration from CodeIgniter's standard file application/config/database.php and you will be able to use any R:: command from anywhere in your code. No includes, not additional code required :-)

Include library PHP Codeigniter

I want to include the Threema library in my Codeigniter project, but I have trouble doing it. This is the library.
I followed the instructions, created an autoload with composer, but I don't know how to make it work. I have an helper that looks like this :
require_once APPPATH . "libraries/Threema/vendor/autoload.php";
//define your connection settings
$settings = new ConnectionSettings(
'*xxxxx',
'xxxxxx'
);
$publicKeyStore = new Threema\MsgApi\PublicKeyStores\File("pathTo/threema_key.php");
$connector = new Connection($settings, $publicKeyStore);
I have the error :
Class 'ConnectionSettings' not found
I tried to do as the guide by using the keyword "use" but that doesn't work either :
use Threema\MsgApi\Connection;
use Threema\MsgApi\ConnectionSettings;
use Threema\MsgApi\Receiver;
What is the simple way to install this library in a CodeIgniter environment ?
try to change this line
require $SERVER['DOCUMENT_ROOT'] ./'libraries/Threema/vendor/autoload.php';

Laravel 5 add nusoap

I am unable to install nusoap to my existing laravel 5 application. Ive done the following:
Created a new Folder in App/Http/Controllers/ - namend "Soap" - and copied the libary into it.
use
composer dump-autoload
So i am able to use
use nusoap_client;
But i am always getting an error:
Class 'App\Http\Controllers\nusoap_client' not found
I thought that laravel automatically load all classes from the "app" directory, but how can i use it here?
Tried with:
$wsdl = "test.xx/_vti_bin/lists.asmx?wsdl";
$client = new nusoap_client($wsdl, true);
Thanks for any help!
Just add these lines to your controller:
include 'nusoap.php';
use nusoap_client;
As a simple way, you can add a backslash in front of nusoap_client, like this:
$client = new \nusoap_client($wsdl, true);

Symfony2: How to override a vendor file

I know how to override anypart of a bundle in Symfony2. I followed this
and it worked.
But what if I want to override a file in the vendor folder which is not part of a bundle.
In my specific example I need to override
vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php
Is this possible? Thanks for your help
You have to tell the EntityManager which Metadata Driver to use:
<?php
$driver = new \Doctrine\ORM\Mapping\Driver\XmlDriver('/path/to/mapping/files');
$em->getConfiguration()->setMetadataDriverImpl($driver);
Instead of the default XmlDriver, you use your extended version, e.g.
<?php
$driver = new \My\XmlDriver('/path/to/mapping/files');
$em->getConfiguration()->setMetadataDriverImpl($driver);
The $em in this snippet is the EntityManager.
See http://docs.doctrine-project.org/en/latest/reference/metadata-drivers.html for further details on how to write and use your own Metadriver implementations.

Categories