PHP: Class not found despite autoloading - php

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?

Related

Autoloading of classes through composer does not work [duplicate]

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"
}
}

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

composer can not find classes and load them

I am using a Dropbox api package and installed it with Composer.When I try to use classes that gives me a fatal error that can not find classes.
This is my composer.json
{
"require": {
"kunalvarma05/dropbox-php-sdk": "^0.2.1"
}
}
This is my php file
use Kunnu\Dropbox\Dropbox;
use Kunnu\Dropbox\DropboxApp;
$app = new DropboxApp("client_id", "client_", 'access_token');
//Configure Dropbox service
$dropbox = new Dropbox($app);
//Get File Metadata
$fileMetadata = $dropbox->getMetadata("/helloworld.txt");
//File Name
// $fileMetadata->getName();
printf($fileMetadata->getName());
My php version is 7.2.4 and the error is:
Fatal error: Uncaught Error: Class 'Kunnu\Dropbox\DropboxApp' not found in D:\MeHDi\Projects\DropBox Api\Upload.php:6
Stack trace:
#0 {main}
thrown in D:\MeHDi\Projects\DropBox Api\Upload.php on line 6
Have you remembered to import composers autoload file using require "path/to/vendor/autoload.php";
This is required in order to initialize and use the different composer libraries. Remember to change the filepath to where your vendor/autoload.php file is located.

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.

Categories