Error accessing class when using Composer autoloader - php

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.

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?

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

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

Setting up php selenium test

i am trying to open firefox as the example explains herehttps://github.com/facebook/php-webdriver/blob/community/example.php with the following code:
<?php
namespace Facebook\WebDriver;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
require_once 'C:\Users\alex\vendor\autoload.php';
$host = 'http://localhost:44441/wd/hub'; // i am running on 44441
$capabilities = DesiredCapabilities::firefox();
$driver = RemoteWebDriver::create($host, $capabilities, 5000);
?>
my composer composer.json at \alex\vendor\ looks like this:
// autoload.php #generated by Composer
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit95fa64adf2a94f75qbb05d3ee5a8af4e::getLoader();
my composer.json looks like this:
{
"require": {
"facebook/graph-sdk": "^5.6"
}
}
and my error is:
PHP Fatal error: Uncaught Error: Class
'Facebook\WebDriver\Remote\DesiredCapabilities' not found in
C:\Users\alex\Desktop\php-webdriver-community\hello.php:11
Stack trace:
#0 {main}
thrown in C:\Users\akal\Desktop\php-webdriver-community\hello.php on line 11
[Finished in 0.0s]
can anyone help?
To use the Facebook web driver, you need to install it using something like...
php composer.phar require facebook/webdriver
(From https://github.com/facebook/php-webdriver#installation )
In the project I use it, this includes a line in the composer.json...
"facebook/webdriver" : "^1.4"
and not the line you have with the graph-sdk

Categories