This question already has an answer here:
Class Foo\Bar\Baz located in ./foo/bar/utility/baz.php does not comply with psr-4 autoloading standard. Skipping
(1 answer)
Closed 1 year ago.
Composer 2.0.1..
I have used version 1 and never had a problem... but now...
The problem is that classes not indexed and php fails with "class not found in..." error.
dump-autoload -o shows no errors and 1193 classes as before I have added last ones.
composer.json section
"autoload": {
"psr-4": {
"Application\\" : "src/"
}
}
class location: src/models/syslog.php
class code:
<?php
namespace Application\Models;
class SysLog extends CustomModel {
}
It is a very simle task... but i have spent a lot of time and it does not work...
Read/write rights are ok.
Autoload adds this line to autoload_psr4.php: 'Application\\' => array($baseDir . '/src'),
So i do not actually have a clue...
The answer is that path in Composer2 MUST be exactly the same as namespace.
path /src/models/syslog.php MUST be /src/Models/SysLog.php for Application
I bet it is wierd but... it is true.
Related
This question already has answers here:
PHP class not found when using namespace
(2 answers)
Class not found in an simple PHP Script
(1 answer)
Closed 1 year ago.
I am following an tutorial on how to create a composer develop envoirement.
https://www.hostinger.com/tutorials/how-to-install-composer
I followed this but am getting the error 'Class "Timer" not found'.
Steps i took:
created a folder for the project
did the command "composer require phpunit/php-timer" on the folder. This automatically created the neccesary composer files.
created an php file called demo.php and copied the code from the tutorial (see code below)
Tried to run the php file with the command "php demo.php" and getting the error.
Code:
<?php
require __DIR__ . '/vendor/autoload.php';
Timer::start();
// your code
$time = Timer::stop();
var_dump($time);
print Timer::secondsToTimeString($time)
The classes in phpunit/php-timer are in a namespace called SebastianBergmann\Timer. Thus, you must either use the fully qualified class name:
\SebastianBergmann\Timer\Timer::start();
Or specify a use alias:
use SebastianBergmann\Timer\Timer;
Timer::start();
// ...
Time::stop();
I haven't looked in much detail but note that the docs for this package do not show static usage, so you might have to do:
use SebastianBergmann\Timer\Timer;
$timer = new Timer();
$timer->start();
// ...
$timer->stop();
I have a project, and I need some packages, so I reorganized the project to use PSR-4.
Here's my composer.json:
{
"name": "me/production",
"type": "project",
"authors": [
{
"name": "Me",
"email": "me#me.com"
}
],
"config": {
"platform": {
"php": "5.6.1"
}
},
"autoload": {
"psr-4": {
"API\\": "api/"
}
},
"require": {
"nesbot/carbon": "^2.25"
},
}
it doesn't work in my scripts. But, here's the real kicker: I do require_once __DIR__ . '/vendor/autoload.php'; in my php console, and it doesn't work either. I have triple and quadruple checked the path, the autoload is there.
What do I mean by "doesn't work"? the autoload requires successfully. It allows me to use libraries. BUT actual instantiations result in a
Class not found error.
Not only for my classes in the API namespace, which are namespaced in the top of the file and are exactly in the folder they're suppose to be, but I also CANNOT instantiate Carbon. I can use Carbon\Carbon but any attempt to instantiate will fail.
Interestingly, instantiation of \Carbon\Carbon directly does not fail.
What's going on? This is weird.
Thank you in advance.
EDIT: I tried redumping the autoloader, I also tried removing the vendor folder and reinstalling. All to no avail.
EDIT: May be worth mentioning I downgraded carbon to ^1.21 because carbon 2 doesnt support php 5.6. But it still doesn't work.
EDIT: It happens with my API namespace as well, here's an example using my implementation of the Instagram API:
use \API\Insta;
php > var_dump(new Insta);
PHP Fatal error: Class 'Insta' not found in php shell code on line 1
php > var_dump(new \API\Insta);
object(API\Insta)#3 (1) {
["ig_token"]=>
string(51) "A_VALID_TOKEN"
}
php >
EDIT: The problem solved itself, it has now mutated into one I care very little about: I can use everything but not in the php console. I am not sure what fixed it.
As you found out, nesbot/carbon, version 2.25 requires at least PHP v7.1.8.
Just having a use statement doesn't check anything, instead it creates a local alias that can be used. If you try to use something that does not exist, only then would it fail.
Please clarify that you have the following directory structure:
./composer.json
./composer.lock
./Api/Insta.php
and in the file Api/Insta.php:
namespace API; // this is your top namespace name
use Carbon/Carbon; // etc
class Insta { ... }
Having the namespace different to the directory name can lead to confusion.
There will also be the index.php/front-controller that pulls in the vendor/autoload.php file and presumably runs the framework.
I have been beating my head for a couple hours trying to figure out why the autoload is not working for "Authentication\auth()". The "dBase\db()" class is loading just fine, but i'm getting:
Error: Class 'Authentication\auth' not found in
/var/htdocs/dev/test.php on line 8
when calling test.php.
Root composer.json -
"require": {
"geeshoe/dbClass": "dev-develop",
"geeshoe/authClass": "dev-master"
},
"autoload": {
"psr-4": {
"dBase\\": "vendor/geeshoe/dbclass/",
"Authentication\\": "vendor/geeshoe/authClass/"
}
}
authClass.php header -
<?php
namespace Authentication;
use dBase\db;
class auth extends db
{
test.php -
if (file_exists("vendor/autoload.php")) {
require "vendor/autoload.php";
} else {
echo "Dam.. Something went wrong!";
}
$test = new \dBase\db();
$var = new \Authentication\auth();
If someone could point out the obvious to me, that would be great. On a side note, autoload is not specified in the authClass->composer.json file for testing purposes.
The problem here is that in fact you don't use PSR-4. In PSR-4 class name should match file name. For db class its fine because db class is located in db.php file, but auth class is located in authClass.php file and that's the problem. You should update file name to auth.php
You might need to run:
composer dump-autoload
Also keep in mind in real packages, one vendor package has one namespace, so you don't create multiple namespaces for single package but only single
Just recently we decided to upgrade from symfony 2.8 too 3.3 with this being noted I felt it was best to start a fresh symfony project and just pull over some parts of the project and change the key needed items that changed over this major version upgrade. I have worked out most of the issues however currently I get this meaning that I may have more issues that I suspect. here is the current one:
C:\xampp\htdocs\Fresh_Api>php bin/console server:run PHP Fatal error: Uncaught Symfony\Component\Debug\Exception\ClassNotFoundException: Attempted to lo ad class "CommandListener" from namespace "Directive\MultiTenant\MasterBundle\EventListener". Did you forget a "use" statement for another namespace? in C:\xampp\htdocs\Fresh_Api\var\cache\dev\a ppDevDebugProjectContainer.php:580 Stack trace:
#0 C:\xampp\htdocs\Fresh_Api\vendor\symfony\symfony\src\Symfony\Component\DependencyInjection\Contai ner.php(331): appDevDebugProjectContainer->getAppBundle_CommandListenerService()
#1 C:\xampp\htdocs\Fresh_Api\var\cache\dev\appDevDebugProjectContainer.php(965): Symfony\Component\D ependencyInjection\Container->get('app_bundle.comm...')
#2 C:\xampp\htdocs\Fresh_Api\vendor\symfony\symfony\src\Symfony\Component\EventDispatcher\EventDispatcher.php(229): appDevDebugProjectContainer->{closure}()
#3 C:\xampp\htdocs\Fresh_Api\vendor\symfony\symfony\src\Symfony\Component\EventDispatcher\EventDispatcher.php(61):Symfony\Component\EventDispatcher\EventDispatcher->sortListeners('console.command')
#4 C:\x in C:\xampp\htdocs\Fresh_Api\var\cache\dev\appDevDebugProjectContainer.php on line 580
This is of course after I try running php bin/console server:run
I have tried deleting my cache, deleting my autoload file, deleting my vendor file and reinstalling composer. I have tried to look to see if there are any dependencies that are missing. (I could have missed something here)
The file it's talking about is a generated file so something about it is not properly noticing that that class exists.
Here is the CommandListener
namespace Directive\MultiTenant\MasterBundle\EventListener;
use Directive\MultiTenant\MasterBundle\Connection\ConnectionWrapper;
use Directive\MultiTenant\MasterBundle\Entity\Tenant;
use Directive\MultiTenant\MasterBundle\Repository\TenantRepository;
use AppBundle\TenantProviderInterface;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Exception;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\Input\InputOption;
The appDevDebugProjectContainer is a little large so I'll wait to see if there are any response to see if it's even needed as I'm sure it's something else that is causing it not to generate the proper information needed.
thanks for any input you have.
EDIT: This is a link here noting it's the same issue however this exact issue there is already resolved before all this. Here is my code for that section.
"autoload" : {
"psr-4": {
"AppBundle\\": "src/AppBundle"
},
"classmap": [ "app/AppKernel.php", "app/AppCache.php" ]
},
This was already fixed already. here is another link I used to try to solve my issue that of course had no help.
php bin/console symfony commands give a fatal error
and another
Symfony 3.3 Getting ClassNotFoundException
All Credit goes to Cerad the answer is sadly duplicated however I did not notice something that was needed. the autoloader needs to know where it is pulling classes from and has to be setup with the new way the symfony does it now. Here is my example with my additional code to make it work.
"autoload" : {
"psr-4": {
"AppBundle\\": "src/AppBundle",
"BundleName\\": "src/BundleName"
},
"classmap": [ "app/AppKernel.php", "app/AppCache.php" ]
},
I got no clue why AWeberAPI is not found. Any help is appreciated.
php code:
require('vendor/autoload.php');
new PHPExcel;
new AWeberAPI;
composer.json:
{
"require": {
"aweber/aweber": "^1.1",
"phpoffice/phpexcel": "^1.8"
}
}
The problem
The module doesn't appear to be properly configured for use/autoloading with composer. They may have just added the composer configuration to allow you to easily install it, but not to use it within the composer autoloader.
The generic convention for it is that AWeberAPI should match the package's PSR-4 autoloader format, which says "look in aweber_api", then it will look for a class named AWeberAPI.php. You can test this behaviour is correct by adding this file:
<?php
// File: vendor/aweber/aweber/aweber_api/AWeberAPI.php
class AWeberAPI {
public function __construct() {
die('yeah, it works now...');
}
}
Then try your script again, the class will exist now.
What can I do?
Well - you could submit a pull request to their repository to fix it, but it looks like it would involve renaming the classes and filenames which would be a breaking change so I probably wouldn't bother.
You can get it to work by requiring the actual source of the API library instead of the composer autoloader in this case:
require_once 'vendor/aweber/aweber/aweber_api/aweber_api.php';