php composer autoload class not found error - php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//Include Composer's autoloader
include 'vendor/autoload.php';
public function test_auth() {
try{
$hybridauth = new Hybridauth\Hybridauth($config);
//Attempt to authenticate users with a provider by name
$adapter = $hybridauth->authenticate('Twitter');
//Returns a boolean of whether the user is connected with Twitter
$isConnected = $adapter->isConnected();
//Retrieve the user's profile
$userProfile = $adapter->getUserProfile();
//Inspect profile's public attributes
var_dump($userProfile);
//Disconnect the adapter
$adapter->disconnect();
}
catch(\Exception $e){
echo 'Oops, we ran into an issue! ' . $e->getMessage();
}
}
An uncaught Exception was encountered
Type: Error
Message: Class 'Hybridauth\Hybridauth\Hybridauth' not found
Filename: C:\xampp\htdocs\paymatrix_v2\application\controllers\Hauth.php
Line Number: 35
Backtrace:
File: C:\xampp\htdocs\paymatrix_v2\index.php
Line: 294
Function: require_once
composer.json file
{
"description": "The CodeIgniter framework",
"name": "codeigniter/framework",
"type": "project",
"homepage": "https://codeigniter.com",
"license": "MIT",
"support": {
"forum": "http://forum.codeigniter.com/",
"wiki": "https://github.com/bcit-ci/CodeIgniter/wiki",
"irc": "irc://irc.freenode.net/codeigniter",
"source": "https://github.com/bcit-ci/CodeIgniter"
},
"require": {
"php": ">=5.2.4",
"mailgun/mailgun-php": "^2.1",
"php-http/curl-client": "^1.6",
"guzzlehttp/psr7": "^1.3",
"aws/aws-sdk-php": "3.*",
"pipl/piplapis-php" : "^5.0",
"hybridauth/hybridauth": "^2.9"
},
"require-dev": {
"mikey179/vfsStream": "1.1.*",
"aws/aws-sdk-php": "dev-master"
},
"autoload": {
"classmap": ["vendor/pipl/piplapis-php/src","vendor/pipl/"]
}
}
autoload.php
// autoload.php #generated by Composer
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit9da23362304113093d59b5cbcc0e2b35::getLoader();
hybrid auth location
vendor/hybridauth/hybridauth/

This file already included in codeigniter core file. dont need to incluse again in your class file
include 'vendor/autoload.php';
Within your function
public function test_auth() {
// Before code
$config = [
'callback' => 'https://example.com/path/to/script.php',
'keys' => [ 'key' => 'your-twitter-consumer-key', 'secret' => 'your-twitter-consumer-secret' ]
];
try {
$twitter = new Hybridauth\Provider\Twitter($config);
$twitter->authenticate();
$accessToken = $twitter->getAccessToken();
$userProfile = $twitter->getUserProfile();
$apiResponse = $twitter->apiRequest( 'statuses/home_timeline.json' );
}
catch(\Exception $e){
echo 'Oops, we ran into an issue! ' . $e->getMessage();
}
}
Check the usage from the package README
https://github.com/hybridauth/hybridauth

I have also faced the same issue. When I install a package using the composer on the root location then it was still saying that the third party class not found.
So here is my solution.
First of all, I change the "composer_autoload" config to TRUE.
$config['composer_autoload'] = TRUE;
By default after change the above configuration Codeigniter looking vendor/autoload file under APPPATH(application folder) which is incorrect. So I change the constant to FCPATH(root path) because the root is the correct path to install the third-party packages using the composer.
change from
System/core/Codeigniter.php:165
if ($composer_autoload === TRUE)
{
file_exists(APPPATH.'vendor/autoload.php')
? require_once(APPPATH.'vendor/autoload.php')
: log_message('error', '$config[\'composer_autoload\'] is set to TRUE but '.APPPATH.'vendor/autoload.php was not found.');
}
to
if ($composer_autoload === TRUE)
{
file_exists(FCPATH.'vendor/autoload.php')
? require_once(FCPATH.'vendor/autoload.php')
: log_message('error', '$config[\'composer_autoload\'] is set to TRUE but '.FCPATH.'vendor/autoload.php was not found.');
}
and finally, run
composer dumpautoload
in case if still not working.
Happy coding!!!

Related

Fatal error when instanciating Pusher - Can't find class

I have a project to test and play around, with the following structure:
app/
controllers/
HomeController.php
handlers/
models/
vendor/
composer/
psr/
pusher/
pusher-php-server/
src/
Pusher.php
PusherException.php
PusherInstance.php
tests/
composer.json
autoload.php
index.php
I tried to require the Pusher autoloader in my index file:
require 'vendor/autoload.php';
Which is the following:
// autoload.php #generated by Composer
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInite16b90ab01042d2a69b1d54243c9e23a::getLoader();
Now, in my HomeController.php, I have the following code:
namespace App\controllers;
use \App\handlers\Views as View;
use \App\models\Home_model as Home;
use \App\controllers\SessionController;
use \Pusher\Pusher as Pusher;
class HomeController {
private $pusher;
public function __construct() {
$options = array(
'cluster' => 'hmm',
'encrypted' => true
);
$this->pusher = new Pusher(
'secret',
'secret',
'secret',
$options
);
}
public function index() {
$data['message'] = 'hello world';
$this->pusher->trigger('my-channel', 'my-event', $data);
return $this->view->render('views/home/index.php');
}
}
But this returns me an error:
Fatal error: Class 'Pusher\Pusher' not found in
And I'm not sure what I'm doing wrong. Could someone explain me what I'm doing wrong?
In composer.json I get the following:
{
"name": "pusher/pusher-php-server",
"description" : "Library for interacting with the Pusher REST API",
"keywords": ["php-pusher-server", "pusher", "rest", "realtime", "real-time", "real time", "messaging", "push", "trigger", "publish", "events"],
"license": "MIT",
"require": {
"php": "^5.4 || ^7.0",
"ext-curl": "*",
"psr/log": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^4.8 || ^5.7"
},
"autoload": {
"psr-4": {
"Pusher\\": "src/"
}
},
"autoload-dev": {
"psr-4": { "": "tests/" }
},
"config": {
"preferred-install": "dist"
},
"extra": {
"branch-alias": {
"dev-master": "3.0-dev"
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
On Github, they mention that the library depends on cURL and JSON modules.
Not realy sure if this has something to do with my issue?
I'm still stuck, so any help is greatly appreciated.
Also, I'm using a .htaccess file, to rewrite my urls.
I have managed your code to work with right composer.json next to index.php:
{
"name": "awesome/project",
"type": "project",
"license": "MIT",
"authors": [
{
"name": "Author",
"email": "author#gmail.com"
}
],
"autoload": {
"psr-4": {
"": ""
}
},
"require": {
"pusher/pusher-php-server": "dev-master"
}
}
Then just run composer install.
My index.php contents:
<?php
require 'vendor/autoload.php';
use app\controllers\HomeController;
$ctrl = new HomeController();

Composer class not Found Error

I've been working on a project and for which I needed to make a package. But it always returns
PHP Fatal error: Uncaught Error: Class 'youtubetomp3\downloader' not found
Here the structure of directory.
youtubetomp3/
src/
downloader.php
test/
downloaderTest.php
composer.json
composer.lock
and other files
The composer.json contains following details.
{
"name": "princeyadav05/youtubetomp3",
"description": "Downloads mp3 of a video given video-id",
"keywords": ["youtube", "songs", "downloader", "package"],
"license": "",
"authors": [
{
"name": "Prince Yadav",
"email": "princeyadav96#gmail.com"
}
],
"type": "package",
"require": {
"php": ">=5.4",
"php-ffmpeg/php-ffmpeg": "^0.11.0"
},
"require-dev": {
"phpunit/phpunit": "5.2.*"
},
"autoload": {
"psr-4": {
"princeyadav05\\youtubetomp3\\": "src/"
}
}
}
And this is how I'm creating object of class.
<?php
require 'vendor/autoload.php';
require 'scrapper.php';
include 'database.php';
echo "** WELCOME TO THE MP3 DOWNLOADER ** \n \n";
$name = readline("Hey There. Lets start with your name : ");
echo "\nHello " . $name . ".\n";
$search = readline("Please enter the search query : ");
$data_array = searchVideo($search); //returns data array
displayVideos($data_array); // returns video id
$download = new downloader();
$video_details = $download->downloadSong($video_id);
// returns array with video title, Duration, url, path
savingToDb($video_title, $video_duration, $video_url, $songs_path);
?>
Please help. I've tried many things but nothing worked.I'm really frustrated.
It's not clear whether you're including autoload.php file generated by composer. Would you mind posting the full code?
Then I'm not sure about your autoload directive, I would try something like this:
"autoload": {
"classmap": [
"src/"
]
},
see composer docs for more info

Fatal error: Class 'Slim\Views' not found in ...vendor\slim\views\Twig.php

I'm building a simple website in PHP with Slim Framework and Twig template engine.
I've installed Slim and Twig with Composer in the Command Line.
This is my index.php
<?php
require __DIR__ . '/vendor/autoload.php';
date_default_timezone_set('Europe/Copenhagen');
$app = new Slim\App( array (
'view' => new Slim\Views\Twig()
));
$view = $app->view();
$view->parserOptions = array(
'debug' => true
);
$view->parserExtensions = array(
new \Slim\Views\Twig(),
);
$app->get('/', function() use($app){
$app->render('about.twig');
});
$app->get('/contact', function() use($app){
$app->render('contact.twig');
});
$app->run();
?>
The error message is:
Fatal error: Class 'Slim\Views' not found in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\data\localweb\projects\simple-php-website\vendor\slim\views\Twig.php on line 46
It works without the Twig framework. So I guess the trouble is on loading the Twig. I've tried different variations of this line:
'view' => new Slim\Views\Twig()
But what confuses me is, that the error message is referring to line 46 in the Twig.php - which is in the core of slim.
I've tried reinstalling Twig and Slim several times.
Any suggestions what is wrong?
Much appreciated!
EDIT
This is from my composer.json
{
"name": "tyf5vl/simple-php-website",
"authors": [
{
"name": "My Name",
"email": "my#mail.com"
}
],
"require": {
"monolog/monolog": "^1.17",
"slim/slim": "^3.1",
"twig/twig": "^1.23",
"slim/views": "^0.1.3"
}
}
http://www.slimframework.com/docs/features/templates.html
states that ONLY view is this -> composer require slim/twig-view
you should have final file as
{
"require": {
"slim/slim": "^3.5",
"slim/twig-view": "^2.1"
}
}

500 server error while using Symfony class

Im trying to run Symfony Crawler in my script, but when I try to do that, I get 500 server error. Where is the problem? The code:
use Symfony\Component\DomCrawler\Crawler;
class Yt_downloader
{
private $__parser;
private $__uri;
public function __construct($cfg)
{
$this->__parser = new Crawler();
if ( is_array($cfg) ) {
foreach ( $cfg as $key => $value ) {
$this->{$key} = $value;
}
}
}
public function test()
{
print_r($this->__uri);
}
}
and the action:
require_once APPPATH . 'libraries/Yt_downloader.php';
$downloader = new Yt_downloader(array(
'__uri' => 'https://www.youtube.com/watch?v=...'
));
btw, my composer.json:
{
"name": "project",
"description": "",
"license": "MIT",
"require-dev": {
"symfony/css-selector": "~2.8|~3.0"
},
"suggest": {
"symfony/css-selector": ""
},
"autoload": {
"psr-4": { "Symfony\\Component\\DomCrawler\\": "" }
},
"minimum-stability": "dev",
"extra": {
"branch-alias": {
"dev-master": "3.0-dev"
}
}
}
this is being used in Codeigniter project. I thought it can be composer problem, but when I try to use different library it works. I think there is a problem with namespaces or something. Maybe I can see log anywhere? I use ubuntu..
You need to require
"symfony/dom-crawler": "~2.8|~3.0"
In your composer.json file so that the crawler component is installed

Composer update not reflecting repository changes

I have the follow composer.json
{
"name": "mjohnson/transit",
"type": "library",
"description": "A file uploader, validator, importer and transformer library.",
"keywords": [
"transit", "file", "uploader", "validator", "importer", "transformer", "transporter",
"image", "audio", "video", "text", "application", "archive", "s3", "glacier"
],
"homepage": "http://milesj.me/code/php/transit",
"license": "MIT",
"authors": [
{
"name": "Miles Johnson",
"homepage": "http://milesj.me"
}
],
"require": {
"php": ">=5.3.0",
"ext-curl": "*",
"ext-mbstring": "*",
"aws/aws-sdk-php": "2.0.*"
},
"support": {
"source": "https://github.com/milesj/php-transit"
},
"autoload": {
"psr-0": { "Transit": "src/" }
}
}
when I run a composer update the source code is not updated to reflect current repository: https://github.com/milesj/transit
I tried to delete lock file whitout success. Tried composer [update|install}
For instance, my current (local) code:
src/Transit/File.php:
[...]
public function __construct($path) {
if (!file_exists($path)) {
throw new IoException(sprintf('%s does not exist', $path));
}
$this->_path = $path;
}
[...]
current repository code:
[...]
public function __construct($path) {
if (is_array($path)) {
if (empty($path['tmp_name'])) {
throw new IoException('Passing via array must use $_FILES data');
}
$this->_data = $path;
$path = $path['tmp_name'];
}
if (!file_exists($path)) {
throw new IoException(sprintf('%s does not exist', $path));
}
$this->_path = $path;
// #version 1.3.2 Rename file to add ext if ext is missing
if (!$this->ext()) {
$this->rename();
}
// #version 1.4.0 Reset the cache
$this->_cache = array();
}
[...]
You have the wrong composer.json. The one you mention is for the library named "mjohnson/transit" - if you are not developing this exact software, then this is wrong.
You should create a new composer.json file containing at least this line:
{ "require": { "mjohnson/transit" : "*" } }
Then run composer install.
I do not know what you did to get that composer.json file, but if you originally cloned that other repository, and now edit that file, things will break! Backup your code if any. Try to undo what you did wrong without undoing your own code.

Categories