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");
Related
I'm very new in the field and I'm trying to create my first composer package. I'm following the structure mentioned here but for some reason I always get that the class is not found.
My directory structure is
Project
- src/
-- project
index.php
- vendor/
-- composer/
autoload.php
index.php
So in the main directory Project I have index.php with
<?php
use App\project;
// Autoload files using the Composer autoloader.
require_once __DIR__ . '/vendor/autoload.php';
$entry = new simplePrint();
echo($entry->printHome());
In the directory src/project/ I have index.php with
<?php
namespace App\project;
class simplePrint {
public function printHome() {
return "Hey";
}
}
in composer.json
"autoload": {
"psr-4": {
"App\\": "src/"
}
}
After I create the files, I've made
composer install
composer dump-autoload
What I'm missing here?
Update: after composer update it is still same. The output of the composer update
$ composer update
Loading composer repositories with package information
Updating dependencies
Nothing to modify in lock file
Installing dependencies from lock file (including require-dev)
Nothing to install, update or remove
Generating autoload files
127.0.0.1:45046 [500]: GET / - Uncaught Error: Class "App\project" not found in ...
PSR-4 says following: The terminating class name corresponds to a file name ending in .php. The file name MUST match the case of the terminating class name.
So you must call your filename simplePrint.php and not index.php.
Here you can read some more information about PSR-4: https://www.php-fig.org/psr/psr-4/
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'm developing a brand new Wordpress plugin and I would like to use Composer to autoload classes.
Here is the plugin directory heriarchy:
my composer.json content:
{
"autoload": {
"psr-4": {
"G4S_ECommerce\\": "src"
}
}
}
In the directory where composer.json is, on cmd, I execute:
composer install -> this generates the vendor/composer folder and the vendore/autoload.php.
composer composer dumpautoload -o -> outputs "Generated optimized autoload files containing 0 classes"
In the main file G4S_Ecommerce.php I put the following line:
require __DIR__.'/vendor/autoload.php';
In the same file I put
use G4S_Ecommerce\Includes\Ecommerce;
$starter = new Ecommerce();
but it leads me to a Fatal error: Uncaught Error: Class 'G4S_Ecommerce\Includes\Ecommerce' not found
Why the composer dumpautoload -o returns 0 classes? What am I doing wrong?
Thanks
First (it is not obvious from your files structure) you need to set a namespace for your Ecommerce class (i.e., G4S_Ecommerce/Includes)
Second, based on what you've declared in the autoload directive, composer is expecting to find the G4S_Ecommerce folder under the src folder, and in that folder you need to place your php class file with a name identical to the class name (i.e., Ecommerce).
I try to run Flysystem's basic example code for the Local adapter and get a Class 'League\Flysystem\Adapter\Local' not found error. This is my process:
version check:
php -v
PHP 5.5.9-1ubuntu4.23 (cli) (built: Feb 8 2018 21:59:47)
install Flysystem:
composer require league/flysystem
output shows I'm up-to-date (this is my 2nd time running it):
Using version ^1.0 for league/flysystem
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Generating autoload files
now there's a vendor folder in web root. And within ./web/vendor/league/flysystem/src/Adapter$ are these files:
AbstractAdapter.php
AbstractFtpAdapter.php
CanOverwriteFiles.php
Ftpd.php
Ftp.php
Local.php
NullAdapter.php
Polyfill/
SynologyFtp.php
...just showing that it seems to be installed correctly(?) I create one test file and one test directory in my web root:
fly-local.php
myfiles/
Into fly-local.php I paste the text from their docs (https://flysystem.thephpleague.com/docs/adapter/local/):
<?php
use League\Flysystem\Filesystem;
use League\Flysystem\Adapter\Local;
$adapter = new League\Flysystem\Adapter\Local(__DIR__.'/myfiles');
$filesystem = new Filesystem($adapter);
...and change the adapter's root folder to myfiles (is that correct?). Then I run it:
php fly-local.php
It outputs:
PHP Fatal error: Class 'League\Flysystem\Adapter\Local' not found in /[PROJECT DIR]/web/fly-local.php on line 6
PHP Stack trace:
PHP 1. {main}() /[PROJECT DIR]/web/fly-local.php:0
What am I doing wrong?
You used composer, then you need to include the composer autoload.php file.
The fly-local.php should be:
<?php
require __DIR__.'/vendor/autoload.php';
use League\Flysystem\Filesystem;
use League\Flysystem\Adapter\Local;
$adapter = new League\Flysystem\Adapter\Local(__DIR__.'/myfiles');
$filesystem = new Filesystem($adapter);
If you use a framework, you can see it includes the autoload php file for you (index.php, in general). If your test/custom file is not included to framework, you need to include the file manually.
I installed composer and laravel and installed some packages
all work fine, but now I created my own class under the folder services
I gave it name space of Services like that:
namespace Services;
And the class name is UploadToImgurService
I run the composer command:
composer dump-autoload
And in my controller I wrote:
use Services\UploadToImgurService;
But I get this error:
Class 'Services\UploadToImgurService' not found
What did I did wrong?
Is there anything else that I should do with composer for autoloading the service class?
EDIT
I found a solution
I edited y composer.json file and added to psr-4 the line
"Services\\" : "app/services"
But why It didn't workt before? The line :
"App\\": "app/",
was there, maybe it loaded the class but under the app namespace?
If you are using a case sensitive file system, you will need to have the Services folder with upper case S.
Your idea is right, but let me try to explain how the psr-4 autoloading works.
You can define root namespaces in your composer.json file and map it to any project directory. Inside the defined directories, your classes should get the root namespace. The namespace segments after the root are build by your sub directory structure and the class name is equal to the file name (PSR-4 Autoloading).
E.g
"MyNamespace\\WithSubNamespace\\": "cool/project"
cool/project/MyClass.php -> MyNamespace\WithSubNamespace\MyClass
cool/project/SubDirectory/AnotherClass.php -> MyNamespace\WithSubNamespace\SubDirectory\AnotherClass
In Laravel, the app directory is mapped to the App namespace as default. Optionally, you can change the root namespace with the command php artisan app:name [NewRootNamespaceName], but the autoloader only finds classes inside the app directory.
If you create a new directory outside of "app", you have to add the directory to your psr-4 namespace mapping in the composer.json file.
In your example, you define a new root namespace in the existing app directory, so your issue was that the root namespace was unknown and you solved it by adding the line in your composer.json.
This is possible, because psr-4 provides a huge flexibility. But personally, i would not recommend to use different root namespaces in the same project.
I hope i could help and maybe this is also interesting for you: composer.json PSR-4.
maybe you forgot to add this line of code
require_once('vendor/autoload.php');
Once I was getting the error:
Fatal error: Uncaught Error: Class "..." not found
in /var/www/html/... on line ...
I was requiring autoload file this way:
require 'vendor/autoload.php';
After I changed the line to:
require __DIR__.'/vendor/autoload.php';
it started working...
Weird, since the the vendor folder was in the same directory as the file which was calling it.
BTW, I was using Composer 2 and PHP 8.1.