Autoloading of classes through composer does not work [duplicate] - php

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 2 years ago.
I have a project structure:
In the index.php I create 2 new objects:
use App\Controllers\Test;
use Xuborx\Cms\App;
new Test();
new App();
My Test.php
<?php
namespace App\Controllers;
class Test
{
}
My App.php
<?php
namespace Xuborx\Cms;
class App {
}
My autoload object in composer.json:
"autoload": {
"psr-4": {
"App\\Controllers\\": "app/controllers",
"Xuborx\\Cms\\": "vendor/xuborx/cms"
}
}
Object Test created successfully in the index.php, but when I am creating new App, I have an error:
Fatal error: Uncaught Error: Class 'Xuborx\Cms\App' not found in
/home/denis/Coding/xuborx-cms/public/index.php:8 Stack trace: #0
{main} thrown in /home/denis/Coding/xuborx-cms/public/index.php on
line 8
Also, when I run composer dump-autoload -o, I get error:
Class Xuborx\Cms\App located in ./vendor/xuborx/cms/core/App.php does
not comply with psr-4 autoloading standard. Skipping.
I think, I not correct use autoload in composer.json, but I don't understand my error. Please< talk me about it.

App.php are inside /core directory :
autoload": {
"psr-4": {
"App\\Controllers\\": "app/controllers",
"Xuborx\\Cms\\": "vendor/xuborx/cms/core"
}
}

Related

PHP: Class not found despite autoloading

I am using the ps-4 autoloader using Composer.
"autoload": {
"psr-4": {
"App\\":"app/",
"Database\\":"database/"
}
},
So, I have the main index.php file in the root directory like this
require 'vendor/autoload.php';
use App\Server;
$server = new Server();
The root directory has the app folder and a class called Server in it like this
namespace App;
echo "in server<hr>";
class Server{}
I get the echo "in server" so the class file gets included. But I get this error
Fatal error: Uncaught Error: Class "App\Server" not found in /var/www/html/index.php:8 Stack trace: #0 {main} thrown in /var/www/html/index.php on line 8
It looks for a class called "App\Server" instead of "Server". How do I fix this?

MVC using composer autoload psr-4 is not worked for me

I'm trying to create a MVC structure and use composer to autoload everything.
But I keep getting this error:
<b>Fatal error</b>: Uncaught Error: Class 'App\Core\Main' not found in /var/www/html/php-framework/index.php:20
Stack trace:
#0 {main}
thrown in <b>/var/www/html/php-framework/index.php</b> on line <b>20</b><br />
My Structure:
Php-framework
-> src
-> Core
-> Main.php
-> vendor
-> composer.json
-> index.php
composer.json file
"psr-4": {
"App\\":"src/"
}
Main.php file
namespace App\Core;
Class Main{
public static function run() {
index.php file
require __DIR__ . "/vendor/autoload.php";
App\Core\Main::run();
but it show me error
This is d my first question on stackoverflow
Check your vendor/composer/autoload_psr4.php file, you must have line like
'App\\' => array($baseDir . '/src'),
If you have not this line try composer dump-autoload (https://getcomposer.org/doc/03-cli.md#dump-autoload-dumpautoload-)
Yup!!
I have resolved the bug myself. I don't know how, but it's working fine.
I followed the steps below:
Remove vendor folder
Run composer dump-autoload -o

Namespace autoloading with composer and PSR-0 not working as expected

I have made a test project to understand how composer and packagist works. The project is also on packagist.
A simple composer require rakibtg/gowin will install the package from packagist.
But for some reason the namespacing is not working as expected.
Here is my directory structure and the composer file.
Here is the GoWin.php file:
<?php
namespace GoWin;
class GoWin {
public function serve() {
echo 'Lets Win Everybody!';
}
}
Here is the test.php file where i am trying to use the serve() method from the GoWin class.
<?php
require_once './vendor/autoload.php';
// use GoWin;
( new GoWin\GoWin() )->serve();
But it fails to execute the serve method with this error:
Fatal error: Uncaught Error: Class 'GoWin\GoWin' not found in
/Users/usr/Desktop/t estGoWin/index.php:7 Stack trace:
0 {main} thrown in /Users/usr/Desktop/testGoWin/index.php on line 7
At this moment i cant understand what i am missing! Also should i use psr-0 or psr-4?
I solved it by switching to PSR-4, simply update the composer.json autolaod property as this:
"autoload": {
"psr-4": {
"GoWin\\": "src/"
}
},

Error accessing class when using Composer autoloader

I have folder app/Controllers/HomeController.php and in my composer autoloader i write like this:
"autoload": {
"psr-4": {
"App\\": "app/",
}
},
but when i try to access my file from public/index.php like this:
require __DIR__ . '/../vendor/autoload.php';
$home = new \App\Controllers\HomeController;
I got some error like this:
Fatal error: Uncaught Error: Class 'HomeController' not found in E:\laragon\www\slim\public\index.php:14 Stack trace: #0 {main} thrown in E:\laragon\www\slim\public\index.php on line 14
so where i'm doing wrong here? for more info in my HomeController i using namespace like this:
namespace App\Controllers;
Ok the answer is I have to dumb my autoloader and it works.

Symfony3: Fatal error: Class 'AppKernel' not found in .\bin\console

just started working on a project, I ran composer update and was greeted with a exception when trying to clear cache.
When I try to run php bin\console server:run I am greeted with this message:
php bin\console server:run
PHP Fatal error: Uncaught Symfony\Component\Debug\Exception\FatalThrowableError: Fatal error: Class 'AppKernel' not found in CoreBundle\bin\console:27
Stack trace:
#0 {main}
thrown in CoreBundle\bin\console on line 27
Fatal error: Uncaught Symfony\Component\Debug\Exception\FatalThrowableError: Fatal error: Class 'AppKernel' not found in CoreBundle\bin\console on line 27
Symfony\Component\Debug\Exception\FatalThrowableError: Fatal error: Class 'AppKernel' not found in CoreBundle\bin\console on line 27
Call Stack:
0.0112 427536 1. Symfony\Component\Debug\ErrorHandler->handleException() CoreBundle\vendor\symfony\symfony\src\Symfony\Component\Debug\ErrorHandler.php:0
Another strong possibility, particularly if the project has been updated from a v2.7 project (or before) is that the AppKernel is just not known to Composer. It is now best practice to not manually require/include the file, (so those lines are removed from web/app_*.php, and bin/console.php). but instead have it autoloaded. However, this requires a line in Composer so that it can be found. Only the composer autoloader would ever be included manually, which can then load everything else.
"autoload": {
"files": ["app/AppKernel.php"],
"psr-4": { // etc...
There are two possible reasons for this.
First your autoloader is trashed or can't find files
composer dump-autoload
Second reason could be, that your var directory isn't writable in which the cache file for symfony is placed.
Just check the permissions.
Also take a close look into the log files.
Maybe it tells you what the real Problem is.
(Like an issue with syntax etc.)
For some reason, I had to explicitly add src/Kernel.php to autoload. So my composer.json autoload section became:
"autoload" : {
"files": ["app/AppKernel.php",
"src/Kernel.php"],
"psr-4": { "": "src/"},
"classmap": [
"app/AppKernel.php",
"app/AppCache.php"
]
}
After adding this, running composer dump-autoload did the trick.

Categories