Unable To Call a Class Installed Using Composer - php

I have installed a PHP wrapper library using Composer. The autoloader seems to be working fine, but I cannot call a class as it says
class 'Diffbot' not found.
I have tried numerous tricks, especially those mentioned in the Composer documentation, but I cannot get it working and I think I must share my problem here.
My composer.json contains the following lines
{
"require": {
"swader/diffbot-php-client": "^0.4.4"
}
}
Directory structure
Vendor
---composer
---guzzlehttp
---react
---swader
---autoload.php
'swader' folder
---diffbot-php-client
---src
---Abstracts
---Api
---Entity
---Exceptions
---Factory
---Interfaces
---Traits
---Diffbot.php
I am trying to call Diffbot class under Diffbot.php, it contains the following namespaces:
namespace Swader\Diffbot;
use Swader\Diffbot\Api\Crawl;
use Swader\Diffbot\Api\Custom;
use Swader\Diffbot\Api\Search;
use Swader\Diffbot\Exceptions\DiffbotException;
use Swader\Diffbot\Api\Product;
use Swader\Diffbot\Api\Image;
use Swader\Diffbot\Api\Analyze;
use Swader\Diffbot\Api\Article;
use Swader\Diffbot\Api\Discussion;
use GuzzleHttp\Client;
use Swader\Diffbot\Factory\Entity;
use Swader\Diffbot\Interfaces\Api;
use Swader\Diffbot\Interfaces\EntityFactory;
/**
* Class Diffbot
*
* The main class for API consumption
*
* #package Swader\Diffbot
*/
class Diffbot
{
/** #var string The API access token */
protected static $token = null;
The autoload_psr4.php file under composer/ folder:
// autoload_psr4.php #generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'Swader\\Diffbot\\' => array($vendorDir . '/swader/diffbot-php-client/src'),
'React\\Promise\\' => array($vendorDir . '/react/promise/src'),
'GuzzleHttp\\Stream\\' => array($vendorDir . '/guzzlehttp/streams/src'),
'GuzzleHttp\\Ring\\' => array($vendorDir . '/guzzlehttp/ringphp/src'),
'GuzzleHttp\\' => array($vendorDir . '/guzzlehttp/guzzle/src'),
);
I am trying to call the Diffbot class from a php script that resides in the same directory as the vendor/ folder in the following manner:
require_once ('vendor/autoload.php');
error_reporting(E_ALL);
$diffbot = new Diffbot();
Edit
I solved my problem. I just added the following lines. I was confused about PHP namespace.
require_once __DIR__.'/vendor/autoload.php';
$foo = new \Swader\Diffbot\Diffbot('foo');

Try
require_once __DIR__.'/vendor/autoload.php';
use Swader\Diffbot\Diffbot;
$diffbot = new Diffbot();
See PHP doc for reference.

Related

PHP Custom Namespace not working

I just finished building a site locally via Xampp. Everything is working just fine, its using PHP version 5.6. I used composer to use some third party apps such as Guzzle and Stringy. After done I uploaded to my Godaddy webhosting account, which uses PHP 5.5. When I load the site I get this error:
Fatal error: Class 'Libs\Model\Site_Settings' not found in public_html/portal/conf/settings.php on line 81
However the vendor namespaces work just fine. I dont get any errors. Heck I dont get any errors with my custom classes. I am using composer to autoload everything. Everything works perfectly locally, just any classes that I use custom namespaces for do not work only on my web hosting account. In my classes I have at the top:
namespace Libs\Model;
I also tried using brackets
namespace Libs\Model {\\code here}
Tried researching the issue, coming up with nothing. Any suggestions? In the psr4 autoload file it shows:
'Libs\\' => array($baseDir . '/lib')
I verified $baseDir is pointed to the correct folder.
UPDATE
Here is code from the class Im trying to call. Very simple:
namespace Libs\Model;
class Site_Settings {
private $dbconn;
public function __construct($dbconn)
{
$this->dbconn = $dbconn;
}
public function findSiteSettings($domain)
{
//We clean any variables being passed to the query
$domain = $this->dbconn->escape($domain);
//We turn on query caching
$this->dbconn->cache_queries = TRUE;
//This is the query statement to run
$query = $this->dbconn->get_row("
SELECT
jp.*,
js.stateabb,
js.statename,
js.statecountry
FROM
job_site AS jp
INNER JOIN
job_state AS js
ON
jp.stateid = js.id
WHERE
jp.sitedomain = '$domain'
AND
jp.active = 1
LIMIT
1
");
//We turn off query caching
$this->dbconn->cache_queries = FALSE;
//We now return any rows found
return $query;
}
}
This is how Im calling it:
//We include the autoloader that is needed to load all vendors for this site
include(VENDORS .'autoload.php');
//We get the site settings for this job site
$settings = new Libs\Model\Site_Settings($global_db);
$site_settings = $settings->findSiteSettings($global_sitedomain);
This is my autoload file for psr4 from composer:
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname(dirname($vendorDir));
return array(
'Symfony\\Polyfill\\Mbstring\\' => array($vendorDir . '/symfony/polyfill-mbstring'),
'Stringy\\' => array($vendorDir . '/danielstjules/stringy/src'),
'Psr\\Http\\Message\\' => array($vendorDir . '/psr/http-message/src'),
'Libs\\' => array($baseDir . '/lib'),
'League\\Plates\\' => array($vendorDir . '/league/plates/src'),
'GuzzleHttp\\Psr7\\' => array($vendorDir . '/guzzlehttp/psr7/src'),
'GuzzleHttp\\Promise\\' => array($vendorDir . '/guzzlehttp/promises/src'),
'GuzzleHttp\\' => array($vendorDir . '/guzzlehttp/guzzle/src'),
'Cocur\\Slugify\\' => array($vendorDir . '/cocur/slugify/src'),
);
UPDATE #2
Here is my composer file
"autoload": {
"classmap": [
"lib/vendor/ezsql/mysqli/ez_sql_mysqli.php",
"lib/vendor/ezsql/shared/ez_sql_core.php",
"lib/helper/url.php",
"lib/helper/html.php",
"lib/helper/form_message.php",
"lib/helper/email_generator.php",
"lib/helper/pagination.php"
],
"psr-4": {"Libs\\": "lib"}
},
"require": {
"league/plates": "^3.1",
"guzzlehttp/guzzle": "^6.2",
"phpmailer/phpmailer": "^5.2",
"cocur/slugify": "^2.1",
"danielstjules/stringy": "^2.3",
"wixel/gump": "^1.3",
"jwage/purl": "^0.0.7"
}
}
You haven't provided enough information to really answer your question. Where is the php file located for Libs\Model\Site_Settings? Are you working on a case insensitive system like OSX and uploading to a case sensitive one like Linux? What auto-loading standard are you using? It looks like you are using both PSR-0 and PSR-4.
You may need to add a autoload path to your composer.json:
"autoload": {
"psr-4": {
"Libs\\Model\\": "lib/src/model",

Class 'GoogleSession' not found when using composer

I'm using the google-trends library and installed it with composer
composer update jonasva/google-trends
my composer.json:
{
"require": {
"jonasva/google-trends": "dev-master"
}
}
I included the file start.php in the main folder:
require __DIR__ . '/vendor/autoload.php';
$config = [
'email' => 'myemail#gmail.com',
'password' => 'mypass',
];
$session = (new GoogleSession($config))->authenticate();
$response = (new GoogleTrendsRequest($session))
->setDateRange(new \DateTime('2014-02-01'), new \DateTime()) // date range, if not passed, the past year will be used by default
->setLocation('BE') // For location Belgium
->getTopQueries() // cid (top queries)
->send(); //execute the request
$data = $response->getTermsObjects(); // return an array of GoogleTrendsTerm objects
But I get
Fatal error: Class 'GoogleSession' not found in
Should I include files other than vendor/autoload.php?
The author conveniently didn't mention the fact that the actual fully qualified class name is Jonasva\GoogleTrends\GoogleSession.
use Jonasva\GoogleTrends\GoogleSession; at the top of your file.
Check the source code of the library to figure out such information.
You have to use FQDN. Namespace + Classname
$session = (new Jonasva\GoogleTrends\GoogleSession($config))->authenticate();

Fatal error HelperSet not found when setting up Doctrine Console (with CodeIgniter)

I've got a problem setting up Doctrine with CodeIgniter. When running the following error is coming up:
Fatal error: Class 'Symfony\Component\Console\Helper\HelperSet' not found in /Applications/MAMP/htdocs/CodeIgniter-2.2.1/application/doctrine.php on line 21
The folder structure looks like this
/application/
/application/doctrine.php
/application/libraries/
/application/libraries/Doctrine/
/application/libraries/Doctrine/Common
/application/libraries/Doctrine/DBAL
/application/libraries/Doctrine/ORM
/application/libraries/Doctrine/Symfony
/application/libraries/Doctrine/Doctrine.php
/application/libraries/Doctrine/index.html
This is the line 21
$helperSet = new \Symfony\Component\Console\Helper\HelperSet(array(
'db' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($em->getConnection()),
'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($em)
));
Cannot find out what is the problem..
Followed this tutorial: http://wildlyinaccurate.com/integrating-doctrine-2-with-codeigniter-2/
update This is my doctrine.php in the application folder
<?php
define('APPPATH', dirname(__FILE__) . '/');
define('BASEPATH', APPPATH . '/../system/');
define('ENVIRONMENT', 'development');
chdir(APPPATH);
require __DIR__ . '/libraries/Doctrine.php';
foreach ($GLOBALS as $helperSetCandidate) {
if ($helperSetCandidate instanceof \Symfony\Component\Console\Helper\HelperSet) {
$helperSet = $helperSetCandidate;
break;
}
}
$doctrine = new Doctrine;
$em = $doctrine->em;
$helperSet = new \Symfony\Component\Console\Helper\HelperSet(array(
'db' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($em->getConnection()),
'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($em)
));
\Doctrine\ORM\Tools\Console\ConsoleRunner::run($helperSet);
Just downloaded github version and without any extra settings everything seems to be ok.
Check if the HelperSet.php file exists in Doctrine/Symfony/Component/Console/Helper directory, and add these lines to Doctrine.php in libraries directory (you have to register Symfony classes):
$symfonyClassLoader = new ClassLoader('Symfony', APPPATH.'your_path_to/Doctrine');
$symfonyClassLoader->register();
This GitHub repository is for CodeIgniter 2, don't use it.
Try this tutorial, it works fine for me, even for CI 3.

Phalcon unable to find class

Hello all im trying to load multiple libraries which is in different folders in library folder using namespaces but i keep getting not found
My directory structure is like this
app/
controllers/
models/
library/
views/
My loader.php is like this
$loader = new \Phalcon\Loader();
/**
* We're a registering a set of directories taken from the configuration file
*/
$loader->registerNamespaces(array(
'Test\Name' => __DIR__ . "/../library/",
));
$loader->registerDirs(
array(
$config->application->controllersDir,
$config->application->modelsDir
)
)->register();
And my basecontroller is trying to call like this
$var = new Test\Name\functions();
and btw the file functions in library is like this
class functions extends Phalcon\Mvc\User\Component
{
public function __construct()
{
}
public function initialize()
{
}
public function checking(){
echo 'checks';
}
}
i Keep getting
Fatal error: Class 'Test\Name\functions' not found in C:\wamp\www\app\controllers\ControllerBase.php on line 38
Any help is appreciated guys thnx
I think that your class should have:
namespace Test\Name;
class functions extends Phalcon\Mvc\User\Component
{
// ... rest of it
on top.
I would also make this configuration:
$loader->registerDirs(
array(
$config->application->controllersDir,
$config->application->modelsDir,
__DIR__ . "/../library/",
)
)->register();
So your class would be in (also I would rename your class to Functions:
app/library/Test/Name/Functions.php
So it would be obvious that your Functions class is in Test\Name namespace.
Having trouble finding the discussion that clued me in but, my understanding is that when using namespaces, you use namespaces. When using directories and other rules, you don't use namespaces.
Namespaces are faster so probably best to just stick with them, dropping the registerDirs as they are superfluous and mean the same thing as the namespaces:
library\Test\Name.php
becomes:
$loader->registerNamespaces(array(
'Apps\Module\Controllers' => $config->application->controllersDir,
'Apps\Module\Models' => $config->application->modelsDir,
'Test' => __DIR__ . "/../library/Test",
));
Then available as Test\Name.
$loader->registerNamespaces(array(
'App' => __DIR__ . "/../library/",
), true);
it will merge all sub dir, if you librarry dir architecture like this
library
- Test
- Name.php
You can call new \App\Test\Name();

Symfony autoloader component

I'm getting:
Fatal error: Class 'Twig_Loader_Filesystem'
<?php
require_once "library/Symfony/Component/ClassLoader/UniversalClassLoader.php";
use Symfony\Component\ClassLoader\UniversalClassLoader;
$loader = new UniversalClassLoader();
$loader->registerNamespace("Symfony\Component", "library/Symfony/Component");
$loader->registerPrefix("Twig_", "library/Twig");
$loader->register();
$loader = new Twig_Loader_Filesystem('templates');
$twig = new Twig_Environment($loader, array(
'cache' => '',
));
?>
The Twig folder is in library folder. Have I missunderstood on how to use the component?
Are you sure your path is correct?
Try to use __DIR__ . '/library'
Try to use DebugUniversalClassLoader to pin down the problem - you can catch a RuntimeException and see which file it actually tries to load.
EDIT:
Correct solution: If you try to load PEAR-style classes with prefix Twig_ from '/library/Twig', you should point it to '/library', because Twig_ prefix itself will be used as a directory name inside /library

Categories