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"
}
}
Related
Zend Framework Version: 1.21.2
I'm updating my app and in particular a package doctrine/mongodb-odm. I run into the above error.
my .yml files are defined like thus:
/configs/doctrinemappings/
- myFile.dcm.yml
- myFile1.dcm.yml
Bootstrap.php
<?
$driver = new \Doctrine\ODM\MongoDB\Mapping\Driver\YamlDriver(array(APPLICATION_PATH.'/configs/doctrinemappings/'));
$config->setMetadataDriverImpl($driver);
?>
I have
composer.json
"require": {
"php": "^8.1",
"doctrine/mongodb-odm": "^2.4.2", <-- updated from 1.2.5
"doctrine/mongodb-odm-bundle": "^4.4"
},
at my disposal. As the error says, YamlDriver not found. I have
AnnotationDriver, SimplifiedXMLDriver, XMLDriver & AttributeDriver at my disposal with my updated package. Can you give me some advice
or a pointer on how best to switch from yaml to one of the above?
It's my understanding, I'd have to use annotations on all my zf1 models to use docblocks for example instead of my current .yml files/setup.
(Assuming i chose AnnotationDriver option above). I've picked this as an example only and would be open to any of the other options.
I'm getting a fatal server error when trying to create an instance of Googles's TextToSpeechClient class while deploying PHP app on App Engine flex environment. On localhost it works without any issue. Below is the error message:
"NOTICE: PHP message:
PHP Fatal error: Uncaught Error: Class
'Google\Cloud\TextToSpeech\V1\TextToSpeechClient' not found in
/app/web/get_voices2.php:46"
My get_voices2.php
<?php
// includes the autoloader for libraries installed with composer
require __DIR__ . '/vendor/autoload.php';
require_once('includes/dbPDO.php');
// Imports the Cloud Client Library
use Google\Cloud\TextToSpeech\V1\AudioConfig;
use Google\Cloud\TextToSpeech\V1\AudioEncoding;
use Google\Cloud\TextToSpeech\V1\SsmlVoiceGender;
use Google\Cloud\TextToSpeech\V1\SynthesisInput;
use Google\Cloud\TextToSpeech\V1\TextToSpeechClient;
use Google\Cloud\TextToSpeech\V1\VoiceSelectionParams;
use Google\Cloud\Storage\StorageClient;
if (isset($_POST['language']) && isset($_POST['quality'])) {
$storage = new StorageClient();
$language = $_POST['language'];
$quality = $_POST['quality'];
$dsn = getenv('MYSQL_DSN');
$user = getenv('MYSQL_USER');
$password = getenv('MYSQL_PASSWORD');
$dbh = OpenCon($dsn,$user,$password);
echo getListVoices($language, $quality, $dbh);
}
function getListVoices($lan, $quality,$conn) {
$optionData = '<option id = "0" disabled>Select voice</option>';
// instantiates a client on line 46
$client = new TextToSpeechClient(['credentials' => json_decode(file_get_contents('cred.json'), true)]);
$response = $client->listVoices();
$voices = $response->getVoices();
}
Here is my App Engine folder structure. Please mention that app.yaml file is not in the web directory. It's in the same dir as /various and /php-docs-sample
PHP app's web directory structure
My composer.json file:
{
"require": {
"google/cloud-speech": "^1.0.1",
"google/gax": "^1.1",
"grpc/grpc": "^1.4",
"google/protobuf": "^v3.3.0",
"google/auth": "^1.8",
"phpseclib/phpseclib": "^2.0"
}
}
I deploy my project on App Engine by running the command:
gcloud app deploy -version dev
I hope I gave complete information.
According to it's repository, that class is given in the package google/cloud-text-to-speech - but according to your composer.json, you haven't required that package.
Why did you require google/cloud-core in the require-dev section after all? That's a good sign that you use a different set of application-specific classes for your development system than for production. Usually, this should only include stuff that is part of your development (like: debugging tools, test tools), but not those that provide the base of your application
I just follow up my question with a solution that worked. As #Nico Haase mentioned in his answer, after executing:
$ composer require google/cloud-text-to-speech
Instead of:
$ composer require google/cloud-speech
Composer automatically added the following line to composer.json
{
"require": {
"google/cloud-text-to-speech": "^1.0"
}
}
Then the client was instantiated without any problem
$client = new TextToSpeechClient();
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 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();
I am using elasticsearch API php to build search results. I have configured everything in my xampp server. All the libraries downloaded from composer.json. In my composer.json file contains below code
{
"require": {
"elasticsearch/elasticsearch": "~2.0"
}
}
Libraries are successfully downloaded. After that i initialize the elastic search with below code
<?php
require 'vendor/autoload.php';
$client = ClientBuilder::create()->build();
It shows fatal error like as follows
Fatal error: Class 'ClientBuilder' not found in E:\Xampp\htdocs\codeporn\elasticsearch\app\init.php on line 4
So i change the config code as,
require_once 'vendor/autoload.php';
$es = new Elasticsearch\Client([
'hosts' => ['127.0.0.1:9200']]
]);
This also shows error like
Parse error: syntax error, unexpected ']' in E:\Xampp\htdocs\codeporn\elasticsearch\app\init.php on line 10
I am following the below youtube tutorial to build the search
https://www.youtube.com/watch?v=3xb1dHLg-Lk
Please suggest what i went wrong in Elasticsearch - PHP.
My PHP Version is 5.5.9
i have initialize the clientbuilder class, now it works fine
require 'vendor/autoload.php';
use Elasticsearch\ClientBuilder;
$client = ClientBuilder::create()->build();
you need install it with composer
composer require elasticsearch/elasticsearch