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.
Related
i have maybe primitive problem. I created my first package in composer. It is just one class in one namespace.
composer.json:
...
"autoload": {
"psr-4": {
"UrlParser\\": "src/"
}
},
...
and i have this in: src/UrlParser/url.php
<?php
namespace UrlParser;
class Url{
...
everything is OK, i uploaded my package into composer. I install it into my project, but when i call this:
<?php
require_once 'vendor/autoload.php';
$a = new UrlParser\Url("http://localhost/aaa.html");
i get this: Fatal error: Class 'UrlParser\Url' not found in C:\xampp\htdocs\ccc\01\index.php on line 3
I am new in composer and i try to google my problem, but i am lost :)
Thanks
Try this
namespace UrlParser;
$a = new Url("http://localhost/aaa.html");
If it doesn't work, there's probably a problem with autoload that you didn't get the right way
the problem was, that i didnĀ“t do this:
composer dump-autoload -o
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();
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"
}
}
I have a github repository https://github.com/KoulSlou/UPS and I would like to add it to my project.
In project root I created composer.json file and defined the following autoloading properties:
{
"autoload": {
"files": [
"libraries/Ups/Ups.php",
"libraries/Ups/Ups_Base.php",
"libraries/Ups/Ups_Base_Response.php",
"libraries/Ups/Ups_Live_Rates.php"
]
}
}
When I run
php composer.phar install
repository is being downloaded, but it looks like autoloader is not working. When I try to initialize one of the classes
$test = new Ups()
I got the following error:
Fatal error: Class 'Ups' not found in application/....
Did I define "autoload" property incorrectly?
I'd suggest not using the "files" autoloader, because that isn't very automatic - the files mentioned here are ALWAYS included. Replacing it with "classmap" would be better. And then you'd not be required to mention ALL files, but you can simply state the directory you want to have scanned for classes.
Now what I don't see anywhere: Did you initialize Composer's autoloader anywhere? This usually is something like
require "vendor/autoload.php";
Finaly, I have found out what was the problem. composer.json file in the project I was trying to load - UPS library -was invalid. I was able to download files when I ran:
composer.phar install
but it looks like composer.json file was ignored. I found it out when I ran
composer.phar update
and got
No valid composer.json was found
With option -v I got error that "name" is undefined index. So, I simply added "name" field to the composer.json. Final version is:
{
"name":"KoulSlou/UPS",
"autoload": {
"files": [
"libraries/Ups/Ups.php",
"libraries/Ups/Ups_Base.php",
"libraries/Ups/Ups_Base_Response.php",
"libraries/Ups/Ups_Live_Rates.php"
]
}
}
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';