I have a package I created that I included with composer called ShinePHP (https://packagist.org/packages/adammcgurk/shine-php#0.0.4), and it has been working fine being autoloaded etc..., but right now the autoload just all of a sudden shut off. There is no reason for this, I didn't touch the composer.json file, I really didn't touch anything with the library, I'm just getting the error:
Fatal error: Uncaught Error: Class 'ShinePHP\EasyHttp' not found in
/Applications/XAMPP/xamppfiles/htdocs/manager-reporting/src/index.php:12
Stack trace: #0 {main} thrown in
/Applications/XAMPP/xamppfiles/htdocs/manager-reporting/src/index.php
on line 12
Here is how that code is being called:
<?php
declare(strict_types=1);
session_start();
require_once 'vendor/autoload.php';
require_once 'model/Page.php';
require_once 'model/Auth.php';
use ShinePHP\{Crud, CrudException, HandleData, HandleDataException, EasyHttp, EasyHttpException};
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
try {
EasyHttp::checkHttps();
} catch (EasyHttpException $ehe) {
// Google Analytics
echo $ehe->getMessage();
exit;
}
I know it is a problem with this particular library, because I also have PHPMailer installed here with Composer, and I just tried instantiating PHPMailer using:
$mail = new PHPMailer(true);
And it worked no problem.
I've ran:
composer dumpautoload
And got this response:
Generating autoload files
Why is my autoload broken for my ShinePHP package?
Autoloading rules in your package (adammcgurk/shine-php) are incorrect. Since your classes are inside of src/ShinePHP directory your autolading rules should look like that:
"autoload": {
"psr-4": {
"ShinePHP\\": "src/ShinePHP/"
}
},
Related
I have a dedicated server with WHM and cPanel. It already has composer installed on it.
I'm trying to run the following php lines in a webpage:
require __DIR__ . '/../vendor/autoload.php';
// Configure API key authorization: JWT
$config = \Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Authorization', '[my api key]');
$apiInstance = new \Swagger\Client\Api\MessagesApi(
new \GuzzleHttp\Client(),
$config
);
I've created a composer.json in the public_html folder and put the following in it:
{
"autoload": {
"psr-4": { "Swagger\\Client\\" : "lib/" }
}
}
And then I ran composer update in the terminal which seemed to install the dependencies and all the relevant files.
It's seeing the autoload.php file but I'm still getting a class not found error:
Fatal error: Uncaught Error: Class 'Swagger\Client\Configuration' not found in /home/mywebsite/public_html/converter/sms.php:9 Stack trace: #0 {main} thrown in /home/mywebsite/public_html/converter/sms.php on line 9
I've been at this for 4 hours now. What am I doing wrong? I can't find anything online that will guide me in the right direction.
In Composer Basic Usage documentation about autoloading it says
For libraries that specify autoload information, Composer generates a vendor/autoload.php file. You can include this file and start using the classes that those libraries provide without any extra work
So, as #NicoHaase said in the comments, if you installed Swagger with Composer (adding a require key and running composer update, for example) you don't need to specify the autoload path to Swagger in composer.json.
I hate to add more to the noise of "autoload isn't working!!!!!", but I can't seem to get this problem figured out, and I figured getting some fresh eyes on it would get the problem in a lot less time. Here is my index.php file:
<?php
declare(strict_types=1);
require_once 'vendor/autoload.php';
require_once 'model/PageNav.php';
use ShinePHP\{Crud, CrudException, EasyHttp, EasyHttpException, HandleData, HandleDataException};
// ALWAYS serve over encrypted channels
try {
EasyHttp::checkHttps();
} catch (EasyHttpException $ex) {
echo $ex;
}
try {
// check if it's a GET request, if it is, serve page, if not, do nothing
if (EasyHttp::isRequestMethod('GET')) {
$Page = new PageNav('Home', 'view/home.php');
$Page->buildPage();
exit;
}
}
catch (EasyHttpException $ex) {
echo $ex;
}
So obviously I am using a package from composer called ShinePHP (it's one I've made, and I'm still working on the documentation, so I'm just using it for my own projects at the moment, composer just makes package management so easy!)
ANYWAYS...because I'm writing this question, I'm obviously getting the following error:
Fatal error: Uncaught Error: Class 'ShinePHP\EasyHttp' not found in /Applications/XAMPP/xamppfiles/htdocs/KRTY/src/index.php:11 Stack trace: #0 {main} thrown in /Applications/XAMPP/xamppfiles/htdocs/KRTY/src/index.php on line 11
Now, I haven't manually touched the composer.json file, so here it is:
{
"require": {
"adammcgurk/shine-php": "~0.0.1"
},
"autoload": {
"psr-4": {
"ShinePHP\\": "src/"
}
}
}
I'm not getting any errors on requiring the vendor/autoload.php file (and I've tried changing the path to something that doesn't exist like vendor/alkdjfladksf/autoload.php and it throws an error how it should), I'm running PHP version 7.2.7 on XAMPP on Mac OS Mojave. Here is the directory structure, the highlighted index.php file is the one with the code above:
And here is the output of composer dump-autoload -o:
Generating optimized autoload files
And so...to add more to the fire of this question on Stack...How can I get composer to autoload my ShinePHP namespace with the classes as shown in the code?
This dependency does not have any autoloading rules, so Composer does not know where to find ShinePHP\EasyHttp class. You need to add autoloading configuration in composer.json of shine-php package:
"autoload": {
"psr-4": {
"ShinePHP\\": "src/"
}
},
I'm new to Composer and I'm really struggling to auto-load my classes with composer. What am I missing in the following process?
I installed the package in my PHP includes folder (which is outside the document root - I'm not sure if that matters) like this:
composer require monolog\monolog
It stated it completed successfully and I confirmed the project was added to my vendor folder.
My entire composer.json file looks like this:
{
"require": {
"monolog/monolog": "^1.22"
}
}
My entire test file looks like this:
<?php
require_once "vendor/autoload.php";
use Monolog\Logger;
$log = new Logger("name");
?>
And I get this error when I load the page:
Fatal error: Uncaught Error: Class 'Monolog\Logger' not found in C:\Dropbox\Projects\Web\Websites\Instamation\wwwroot\qbtest.php:6 Stack trace: #0 {main} thrown in C:\Dropbox\Projects\Web\Websites\Instamation\wwwroot\qbtest.php on line 6
It includes the vendor/autoload.php file without any error.
I've tried to run these commands in composer without any change:
composer update
composer dump-autoload -0
I've also tried it with different packages and I get the same error, so I'm pretty sure it has nothing to do with the monolog package.
Is there a step here I'm missing? I don't need to manually define which classes to autoload in a json file if I require them in composer, do I?
Edit 1:
As requested, here's the paths to my different files.
Path to the test page:
C:\Dropbox\Projects\Web\Websites\Instamation\wwwroot\qbtest.php
Path to the composer.json file (outside the document root but in my includes path):
C:\Dropbox\Projects\Web\Websites\Instamation\wwwincludes\composer.json
My vendor folder is here:
C:\Dropbox\Projects\Web\Websites\Instamation\wwwincludes\vendor\
And inside my vendor folder I have these folders and file:
bin/
composer/
monolog/
psr/
autoload.php
You need to include autoload in your qbtest.php as following:
require_once "../wwwincludes/vendor/autoload.php";
use Monolog\Logger;
$log = new Logger("name");
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 have just started a new PHP project and right up front, I am running into issues with the autoloader. I searched for the error and consulted the documentation (http://www.php-fig.org/psr/psr-4/), but the issue persists.
Hence, I created a minimal example to narrow down the cause of the error - yet, even with this minimal example, it wont work :(
My folder structure is like this:
+ src/
| + Xyz.php
+ composer.json
+ test.php
Here is my code
composer.json:
{
"name": "sg/ABC",
"description": "abc",
"autoload": {
"psr-4": {
"sg\\ABC\\": "src/"
}
}
}
Xyz.php:
<?php namespace sg\ABC;
class Xyz
{}
?>
test.php:
<?php namespace sg\ABC;
use sg\ABC\Xyz;
$a = new Xyz();
?>
Even though running composer install shows no errors, I immediately get this error when running the code:
$ php test.php
PHP Fatal error: Class 'sg\ABC\Xyz' not found in /dir/x/test.php on line 5
Fatal error: Class 'sg\ABC\Xyz' not found in /dir/x/test.php on line 5
also, running composer dump-autoload (as suggested here and there in this board) does not help
You still need to include the composer autoload.php file to include the loaded libraries.
You need to require the autoloader .. it is usually require_once "path/to/vendor/autoload.php".