using composer and autoload in wordpress theme, 'Failed opening required' - php

So I've installed createsend-php for my theme via composer (I'm trying to learn how too use it) but can't get to the next stage.
I can see the API here -
/wp-content/themes/wonkhe2-theme/vendor/campaign-monitor/createsend-php/
composer file seems right to me -
"require": {
"php": ">=5.4.0",
"composer/installers": "~1.0",
"campaignmonitor/createsend-php": ">=6.0"
}
in /wp-content/themes/wonkhe2-theme/templates/content-signup-cm.php I've added
require_once 'csrest_campaigns.php'
And that returns
Fatal error: require_once(): Failed opening required 'csrest_campaigns.php' (include_path='.:/Applications/MAMP/bin/php/php7.2.7/lib/php') in /wp-content/themes/wonkhe2-theme/templates/content-signup-cm.php on line 5
Should the require_once path be different? I thought autoloader would set the paths and namespaces.
Using composer is new to me so apologies if I'm misunderstanding but any help appreciated.

you should not require individual classes insalled by composer. instead, right at the start of your code:
require_once 'vendor/autoload.php';
then you can just start using objects;
use Some\Class\Or\Other;
$object = new Other();

Related

500 Error when testing monlog package

I'm trying to test out the monolog package using composer (full disclosure: this is my first try using composer) and am getting a 500 error when running this code:
<?php
// composer autoloader
require_once 'vendor/autoload.php';
// Shortcuts for simpler usage
use \Monolog\Logger;
use \Monolog\Formatter\LineFormatter;
use \Monolog\Handler\StreamHandler;
// Common logger
$log = new Logger('files');
// Line formatter without empty brackets in the end
$formatter = new LineFormatter(null, null, false, true);
// Debug level handler
$debugHandler = new StreamHandler('debug.log', Logger::DEBUG);
$debugHandler->setFormatter($formatter);
// Error level handler
$errorHandler = new StreamHandler('error.log', Logger::ERROR);
$errorHandler->setFormatter($formatter);
// This will have both DEBUG and ERROR messages
$log->pushHandler($debugHandler);
// This will have only ERROR messages
$log->pushHandler($errorHandler);
// The actual logging
$log->debug('I am debug');
$log->error('I am error', array('productId' => 123));
?>
I see in Dreamweaver that the three 'use' lines in monolog_test.php are highlighted in red:
The error message is:
In my /Applications/MAMP/logs/php_error.log:
[03-Mar-2018 14:10:05 America/Toronto] PHP Fatal error: Class
'Monolog\Logger' not found in
/Users/Ross/Dropbox/htdocs/wonderfest/secure/contest/monolog_test.php
on line 13
My file system setup looks like this:
I installed Composer globally on my Mac and I know the installation is good because I was able to add some packages using composer require . My composer.json file:
{
"require": {
"monolog/monolog": "1.0",
"phpfastcache/phpfastcache": "^6.1",
"mpdf/mpdf": "^7.0"
}
}
And my vendor\autoload.php:
<?php
// autoload.php #generated by Composer
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit348f040c8a2a7d48c0a311fb1af10c08::getLoader();
I guess my question is this: do I have to do anything other than install packages to make their namespaces available?
I updated monolog to the latest stable version and although the 3 'use' lines in Dreamweaver still show up as errors, it works. What threw me was this line on the monolog package page:
Monolog works with PHP 7.0 or above, use Monolog ^1.0 for PHP 5.3+ support.
I thought that meant that I needed to use v1.0 for my PHP 5.6.32, but when I removed that version constraint from the require command everything seems to work - I get the two logs in the same folder as the monolog_test.php file.
My new composer.json (with one additional packages installed):
{
"require": {
"monolog/monolog": "^1.23",
"mpdf/mpdf": "^7.0",
"phpfastcache/phpfastcache": "^6.1",
"phpmailer/phpmailer": "^6.0"
}
}

pdftk: PHP Fatal error: Class 'mikehaertl\pdftk\Pdf' not found in

I have a project in Slim framework. There are multiple composer packages. Now, I have added https://github.com/mikehaertl/php-pdftk to fill PDF but I'm getting PHP Fatal error: Class 'mikehaertl\pdftk\Pdf' not found in... What is wrong with my code?
composer.json:
{
"require": {
"dibi/dibi": "^3.0",
"slim/slim": "^3.7.0",
"phpmailer/phpmailer": "^5.2",
"jbroadway/urlify":"^1.1.0",
"simplesoftwareio/simple-qrcode":"^1.5.1",
"gopay/payments-sdk-php":"^1.2.3",
"mikehaertl/php-pdftk":"^0.4.2"
}
}
in my ./config/config.php there is:
define('PATH', dirname(__DIR__));
require_once PATH."/vendor/autoload.php";
and in my ./methods/controller.php:
...
$pdf = new mikehaertl\pdftk\Pdf("/path/to/QR_template.pdf");
...
I tried to get names of all included files by using:
var_dump(get_included_files());
but the "pdf" files were not in the array.
Other packages works well but pdftk not. Where is the problem?
Thank you.
Solved! The problem was composer version. Composer self-update solved the problem.

PHP Composer PSR-4 autoloader not working

I'm trying to build a composer package for one of my old libraries. I'm also bit new to GIT. Doing this I'm also learning git workflow. I'm following these articles for building composer package.
1 - http://culttt.com/2014/05/07/create-psr-4-php-package/
2 - https://knpuniversity.com/screencast/question-answer-day/create-composer-package
I've uploaded a test code to Github to know everything working fine. My Github link : https://github.com/mi6crazyheart/youtube-extract
But, It seems like when I'm downloading my package through Composer it's autoloader is not working. Getting following error in my console file -
Uncaught Error: Class 'Youtube\\Extract' not found in /var/www/html/suresh/opensource/test/index.php:4\nStack trace:\n#0 {main}\n thrown in /var/www/html/suresh/opensource/test/index.php on line 4
Code for my index.php file where I'm trying to load this library
<?php
require __DIR__ . '/vendor/autoload.php';
$youtube = new Youtube\Extract();
echo $youtube->greeting();
I'm using the following code in my composer.json file to download code from git repository
{
"require": {
"mi6crazyheart/youtube-extract": "dev-master"
},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/mi6crazyheart/youtube-extract"
}
]
}
Don't know where I'm doing mistake. Need some guidance.
Your namespace is "Youtube\Extract" and your class is "Extract" which means your code to make a new instance of the class Extract needs to look like the following:
<?php
$youtube = new Youtube\Extract\Extract();

How do I require a file or class in a vendor library in Yii2 that is not already included?

I have this class that Yii2 cannot see. All the other classes work. I tried the following. The commented lines did not work.
// use app\vendor\googleads\googleads-php-lib\src\Google\Api\Ads\Common\Util\ErrorUtils;
// require_once UTIL_PATH . '/ErrorUtils.php';
require_once('../vendor/googleads/googleads-php-lib/src/Google/Api/Ads/Common/Util/ErrorUtils.php');
use \ErrorUtils;
This works, but it doesn't look right. Also it doesn't work in the command mode, which I need.
$ yii cron
PHP Warning: Uncaught exception 'yii\base\ErrorException' with message 'require_once(../vendor/googleads/googleads-php-lib/src/Google/Api/Ads/Common/Util/ErrorUtils.php): failed to open stream: No such file or directory' in /cygdrive/c/Users/Chloe/workspace/xxx/models/GoogleAdWords.php:36
How can I require or use this class in Yii2?
Fisrt addto composer (shell command):
$ composer require googleads/googleads-php-lib
Then simply use te class:
\ErrorUtils::GetApiErrors($var);
Note that googleads don't use namespaces so it's "\" NS
The library that you use doesn't provide psr-4 autoloader settings for its classes. You need to add autoload classmap for classes that you want to load, into your composer.json in project's root directory like following:
"autoload": {
"classmap": [
"vendor/googleads/googleads-php-lib/src/Google/Api/Ads/Common/Lib",
"vendor/googleads/googleads-php-lib/src/Google/Api/Ads/Common/Util"
]
},
and then in your console: composer dump-autoload
This will update the composer autoloader. After that you'll be able to call library classes with: \ErrorUtils::GetSourceOperationIndex($error)

CakeResque - Resque not found in ...app/Plugin/CakeResque/Lib/CakeResque.php on line 82

CakeResque - Resque not found in ...app/Plugin/CakeResque/Lib/CakeResque.php on line 82
I've followed the instructions here: http://cakeresque.kamisama.me/install#requirements to setup CakeResque with my CakePHP project but the Resque class isn't loading. I installed CakeResque using Composer and I'm assuming I'm missing something (possible autoloader) but with no luck.
Any help is appreciated. Thanks.
Got help at github. I had missed adding composer autoloader in my app/Config/core.php per the instructions.
make sure the vendor directory is mentioned in the app/composer.json
{
"require": {
"kamisama/cake-resque": "^4.1"
},
"config": {
"vendor-dir": "Vendor"
}
}
in the app/Config/core.php require the autoload
require_once dirname(__DIR__) . '/Vendor/autoload.php';

Categories