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",
Related
I am using PHP 5.5.9 and have installed the library BladeOne in my composer file:
{
"name": "test",
"authors": [
{
"name": "test",
"email": "test#test.com"
}
],
"require": {
"eftec/bladeone": "^3.0",
"davechild/textstatistics": "1.*",
"hassankhan/config": "^1.0"
}
}
I am running my script the following way:
<?php
require "vendor/autoload.php";
Use eftec\bladeone;
use DaveChild\TextStatistics as TS;
$views = __DIR__ . '/views';
$cache = __DIR__ . '/cache';
define("BLADEONE_MODE",1); // (optional) 1=forced (test),2=run fast (production), 0=automatic, default value.
$blade=new bladeone\BladeOne($views,$cache); <----- Here I get the error!
However, I get the following error here:
Fatal error: Class 'eftec\bladeone\BladeOne' not found in /home/ubuntu/workspace/testExample.php on line 10
Any suggestions why the library cannot be used in my script?
As per the docs you are supposed to add the namespace to the autoloading of composer by adding it to your composer.json.
"autoload": {
"psr-4": {
"eftec\\": "vendor/eftec/"
}
}
then, (again, the docs say) run composer update. I suppose though that composer dump-autoload would suffice.
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();
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.
I'm running into some issues trying to figure out how to use Twig in a specific way. I'm trying to write a view class that I can use in my application regardless of what template system is installed, that way the only things I would have to change would be the templates themselves and the view class.
However, when I try and create the twig objects in the class, I get Fatal error: Class 'Template\Twig_Loader_Filesystem' not found errors, and I'm not really sure what I'm missing.
Can someone point me in the right direction?
Here's what I've got so far...
composer.json
{
"name": "Movies",
"description": "Find movies",
"require": {
"ext-curl": "*",
"twig/twig": "~1.0"
},
"autoload": {
"classmap": [
"app/classes"
]
}
}
index.php
require_once 'app/app.php';
use Template\Template;
echo Template::render('hello.html', array('name' => 'bob', 'age' => '33'));
app/app.php
define('APP_DIRECTORY', __DIR__ . '/..');
require_once APP_DIRECTORY . '/vendor/autoload.php';
app/classes/Template.class.php
namespace Template;
class Template {
static function render($template_name, $template_data = array()) {
$loader = new Twig_Loader_Filesystem(APP_DIRECTORY . '/app/templates');
$twig = new Twig_Environment($loader);
return $twig->render('hello.html', array('name' => 'lisa', 'age' => '33'));
}
}
Ok, it turned out whenever I would try and include the twig namespaces on my Template class, I was doing it wrong. Looking at other projects my organization has put together, it turns out just adding
use Twig_Environment;
use Twig_Loader_Filesystem;
to the class is enough. I wasn't sure if I actually had to add anything, since the Autoloader was included, or if I had to include part of the path, like Twig_Twig/Loader/Filesystem or something like that. Obviously neither of those was working.
I try to use the addendum library with Zend Framework 2 but I failed.
I tried to add it as a module by copying the addedum il to my \module\util directory. It dosent work.
Then I try something else. I copied the directory under \vendor and addendum like that:
<?php
// autoload_namespaces.php generated by Composer
$vendorDir = dirname(__DIR__);
$baseDir = dirname($vendorDir);
return array(
'Zend\\' => $vendorDir . '/zendframework/zendframework/library/',
'ZendTest\\' => $vendorDir . '/zendframework/zendframework/tests/',
'Psr\\Log\\' => $vendorDir . '/psr/log/',
'Monolog' => $vendorDir . '/monolog/monolog/src/',
**'Addendum' => $vendorDir . '/addendum/',**
);
It dosen't work
So I tried to add it at the end od the init_autoloader.php like this:
$loader = new Zend\Loader\StandardAutoloader();
$loader->registerNamespace('Addendum', __DIR__ . '/vendor/addendum');
$loader->register();
When I try to instantiate a class like this:
$foo = new \ReflectionAnnotatedClass($obj);
I always have the same error:
PHP Fatal error: Class 'ReflectionAnnotatedClass' not found in MyClass.php
use composer - and add there:
"require": {
"php": ">=5.3.3",
"zendframework/zendframework": "2.*",
"niktux/addendum": "dev-master"
}
then just php composer.phar install
https://github.com/Niktux/addendum
Then composer autoloader should sort it out for you.