I'm using codeigniter framework.
I want to use redis on my project.
to do this I find this library:
https://packagist.org/packages/predis/predis
I installed it with composer.
now I want to use it:
$client = new Predis\Client();
but I got this message:
Message: Class 'Predis\Client' not found
I set up these configs:
$config['composer_autoload'] = '/vendor/autoload.php';
$config['composer_autoload'] = TRUE;
my OS is windows 7. somewhere says I should install redis on my windows because I'm using localhost. yeah?
updated
Move composer.json, composer.lock and vendor inside application directory and leave $config['composer_autoload'] = '' empty. It is recommended since that way someone won't be allowed reading what external libraries/packages your application uses. Also APPPATH.'vendor' is default CI location (by docs).
If you for some reason want to have composer.json in publicly accessible location, try your existing configuration with this change:
$config['composer_autoload'] = FCPATH.'vendor/autoload.php';
Also, have you put
use Predis\Client as PredisClient;//for distinctive use of Client word in case of Guzzle, other libs, etc.
before controller class code? After that you can use it as
$client = new PredisClient();
Check if all of this works.
Related
I was working in a scratch file and tried to use a non native function (muscle memory) and of cause it threw an error
The PHP Fatal error: Uncaught Error: Call to undefined function dd() in .../.scratch.php
What I wanted to know is can I make dd() for example available globally across all projects/scratch files without having to autoload it myself.
Essentially you would set a PSR-4 autoload path or directory to include.
After exploring the settings I didn't come up with a way of doing it so this is what I did instead.
composer global require --dev symfony/var-dumper
<?php
include $_SERVER['APPDATA']. '/Composer/vendor/autoload.php';
$var = '';
dd($var);
I just complete the answer above
To use 'vendor' projects and components in your phpstorm scratch file
include the file vendor/autoload.php
and for the example to check a Symfony component... :
require '/path/to/my/project/app/vendor/autoload.php';
use Symfony\Component\Intl\Currencies;
\Locale::setDefault('sv');
$res = Currencies::getSymbol('SEK');
print_r($res);
Resolving the issue. My Case:
Before reading the question, my issue was solved due to my development environment. Using CodeKit (an application on MacOS), upon building my code from the source folder, items such as the composer.json and other files did not transfer causing the issues described below. If this does happen to you scout the two folder to look for discrepancies the paste the missing docs from the src to the build folder.
:: QUESTION ::
I am starting to use GCP today and after following the instructions defined here:
composer require google/cloud-storage
then:
putenv("GOOGLE_APPLICATION_CREDENTIALS=/path/to/creds.json");
require __DIR__.'/vendor/autoload.php';
use Google\Cloud\Storage\StorageClient;
$myid = "my-project-id";
$storage = new StorageClient([
'projectId'=>$myid
]);
var_dump($storage->buckets());
When running this i get the following error:
Fatal error: Class 'Google\Auth\Cache\MemoryCacheItemPool' not found in /place/to/vendor/google/cloud-core/RequestWrapperTrait.php on line 94
I have no idea how to solve the issue, as i am just getting started with GCP. No idea whether this is a problem with the platform or my code.
File structure appears as follows for the Google Auth:
vendor
google
auth
src
tests
cloud-core
cloud-storage
the /Cache/MemoryCacheItemPool exists inside both the tests and src folder, but the above is referencing it minus the src or tests folder.
I have also ran:
composer update
and uninstalled and reinstalled the package to no effect
Google Cloud Project Link
Where did you find the code that you used and pasted? Because the one present in the official documentation is different.
This portion of code is the one in the tutorial you linked, try to use the client library and post the error logs if get any!
require __DIR__ . '/vendor/autoload.php';
use Google\Cloud\Storage\StorageClient;
$projectId = 'YOUR_PROJECT_ID';
$storage = new StorageClient(['projectId' => $projectId]);
$bucketName = 'my-new-bucket';
$bucket = $storage->createBucket($bucketName);
echo 'Bucket ' . $bucket->name() . ' created.';
Remember that bucket name should be unique and therefore I would advice you to test it with a long complex name to avoid to hit already used names, and always test the result of the operation.
UPDATE
I tested also your code and it is working as well, therefore I believe that is an error in the setup of the environment.
Did you get any error while running the composer require google/cloud-storage? Because the class that is missing Google\Auth\Cache\MemoryCacheItemPool is part of Psr that is installed by the composer
[...]
Installing psr/cache (1.0.1)
Loading from cache
[...]
UPDATE2
Matthew M found the error in its configuration and posted:
Finally resolved the issue. I'm using CodeKit in my working
environment and it looks like it is changing something when it
compiles. Ran an uncompiled version and it's working fine.
I installed composer in my CodeIgniter project, downloaded 2 packages: Aura/Sql and Aura/SqlQuery
this is my code from index.php file
require_once ROOTPATH . 'vendor/autoload.php';
use Aura\Sql\ExtendedPdo;
$db = new ExtendedPdo('mysql:host=127.0.0.1;dbname=mydb', 'root', '', array(), array());
var_dump($db->fetchAll('SELECT * FROM sh_users'));
use Aura\Sql_Query\QueryFactory;
$query_factory = new QueryFactory('mysql');
require_once BASEPATH . 'core/CodeIgniter.php';
both fragments are copied from documentation
var_dump gives perfect result, but QueryFactory gives me error
Fatal error: Class 'Aura\Sql_Query\QueryFactory' not found in F:\XAMPP\htdocs\codeigniter\public\admin\index.php on line 83
and i have no idea why. all vendors are downloaded and all php files are there, but it seems autoload doesnt load it. why?
Take a look at the file structure on disk; you may actually want to be including Aura\SqlQuery\QueryFactory, instead of something under the Sql_Query namespace. It may be something as simple as that. I've encountered issues when I've forgotten to rename the class in a PSR-0 compliant path such that it matches the file name, so if in fact the contents on the disk are in:
Aura\SqlQuery\QueryFactory but your use statement is Aura\Sql_Query\QueryFactory, you'll run into a problem.
As mentioned below in the comments, it appears that the Aura devs have two branches, the master branch on the Githup project auraphp/Aura.Sql_Query still has the directory structure as Sql_Query where as the default package, served by packagist, serves the dev-rename branch which replaces Sql_Query with SqlQuery.
Hope that helps!
I've downloaded the paypal-core-sdk for php from GitHub manually, my server does not have 'composer' and I've just manually copied the files to an include directory outside the document root. Problem is when I go call on some of the classes the server borks and doesn't know what I'm talking about ... i.e.
Fatal error: Class 'PPApiContext' not found in /home/rctoronto/public_html/start.php on line 143
if I include once on the file providing PPApiContext, it moves along to another error and so on eventually proceeds when I've included all the ones it complains about. But then the sdk has internal dependency issues.
How do I formally declare the sdk library so my app can use it - I've scoured the web and haven't managed to find a good answer to this question. I've tried manually altering my include_path using my php.ini and still it can't use the classes of the sdk. I don't have access to /usr/lib/php or /usr/local/lib/php as I am on a shared server.
this is the 'getAuthorize' url for paypal (which I've gotten to work with manually include_once on the files...
$apicontext = new PPApiContext(array('mode' => 'live'));
$clientId = PP_CLIENTID;
$clientSecret = PP_SECRET;
$scope = array('openid', 'email');
$redirectUri = 'https://maskedurl.ca/login?ltype=pp&loginSubmit=true';
$pp_authUrl = PPOpenIdSession::getAuthorizationUrl($redirectUri, $scope , $clientId, $apicontext);
But how do we properly declare the sdk library in whole?
From: http://paypal.github.io/sdk/
If you do not want to use composer, you can grab the SDK zip, unzip the file to your application folder and require the 'vendor/autoload.php' file. This file registers a custom autoloader that can autload the PayPal SDK files.
Download SDK zip
Here's how I solved it. Update composer.json as follows:
{
"name": "PayPal Test",
"require": {
"paypal/sdk-core-php": "dev-stable"
},
"autoload": {
"psr-0": {"PayPal\\": "vendor/paypal/sdk-core-php/lib/"}
}
}
This will do the job:
include('paypal-sdk-core/lib/common/PPApiContext.php');
include('paypal-sdk-core/lib/PPConstants.php');
include('paypal-sdk-core/lib/PPConfigManager.php');
include('paypal-sdk-core/lib/auth/openid/PPOpenIdSession.php');
If you need the full stack to make the example on https://devtools-paypal.com/guide/openid/php?interactive=ON&env=sandbox work, use this:
include('paypal-sdk-core/lib/common/PPApiContext.php');
include('paypal-sdk-core/lib/PPHttpConfig.php');
include('paypal-sdk-core/lib/common/PPUserAgent.php');
include('paypal-sdk-core/lib/PPHttpConnection.php');
include('paypal-sdk-core/lib/PPLoggingLevel.php');
include('paypal-sdk-core/lib/exceptions/PPConnectionException.php');
include('paypal-sdk-core/lib/common/PPModel.php');
include('paypal-sdk-core/lib/auth/IPPThirdPartyAuthorization.php');
include('paypal-sdk-core/lib/handlers/IPPHandler.php');
include('paypal-sdk-core/lib/PPConstants.php');
include('paypal-sdk-core/lib/PPConfigManager.php');
include('paypal-sdk-core/lib/auth/openid/PPOpenIdSession.php');
include('paypal-sdk-core/lib/auth/PPTokenAuthorization.php');
include('paypal-sdk-core/lib/auth/openid/PPOpenIdTokeninfo.php');
include('paypal-sdk-core/lib/transport/PPRestCall.php');
include('paypal-sdk-core/lib/PPLoggingManager.php');
include('paypal-sdk-core/lib/handlers/PPOpenIdHandler.php');
Monolog : http://github.com/Seldaek/monolog
Symfony Class Loader : https://github.com/symfony/ClassLoader
As per the usage instructions on the Monolog site, I'm trying to load the class loader to load monolog in my php project. I cant get 'Composer' installed on my machine (firewall problems from my work machine), so I'm trying to follow the instructions on monolog and symfony sites but I'm having issues.
Here is my SAMPLE of my directory structure with my php code :
myProj/
--ClassLoader/ (symfony)
----UniversalClassLoader.php
--Monolog/
----Formatter/
----Handler/
----Processor/
----Logger.php
--myPhpFile.php
And here is my php code to attempt to 'require' monolog
require_once(realpath('ClassLoader/UniversalClassLoader.php'));
$loader = new Symfony\Component\ClassLoader\UniversalClassLoader();
$loader->register();
$loader->registerNamespace('Monolog', realpath('Monolog'));
require_once(realpath('Monolog\Logger.php')); //exception generated here! :-(
and here is the php exception I'm getting inside the Monolog\Logger.php as soon as it attempts to 'require' monolog
Interface 'Psr\Log\LoggerInterface' not found
but I cant even FIND anything that looks like a 'psr\log' namespace in the monolog code. What bits am I missing?
Psr\Log is a dependency of monolog, if you are not using Composer you'll have to track down all dependencies and it's probably not going to be fun (though for monolog there is only the psr-log package). If you really want you can find it at https://github.com/php-fig/log - but you could also try to download composer via your browser, just grab https://getcomposer.org/composer.phar