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/"
}
},
Related
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"
}
}
I am upgrading laravel from 5.5.* to 5.6.0. Composer install ran fine. I am trying to run unit tests upon which this error is popping up.
Class '..\Unit\UnitTestCase' not found
This is the stack trace of the error:
Fatal error: Uncaught Error: Class 'PhiraterTest\Unit\UnitTestCase'
not found in
/home/vagrant/code/phirater-l51/tests/unit/Phirater/AdditionalCurrencies/CreateAdditionalCurrencyCommandHandlerTest.php:11
Stack trace:
#0 /home/vagrant/code/phirater-l51/vendor/phpunit/phpunit/src/Util/Fileloader.php(64):
include_once()
#1 /home/vagrant/code/phirater-l51/vendor/phpunit/phpunit/src/Util/Fileloader.php(48):
PHPUnit\Util\Fileloader::load('/home/vagrant/c...')
#2 /home/vagrant/code/phirater-l51/vendor/phpunit/phpunit/src/Framework/TestSuite.php(325):
PHPUnit\Util\Fileloader::checkAndLoad('/home/vagrant/c...')
#3 /home/vagrant/code/phirater-l51/vendor/phpunit/phpunit/src/Framework/TestSuite.php(403):
PHPUnit\Framework\TestSuite->addTestFile('/home/vagrant/c...')
#4 /home/vagrant/code/phirater-l51/vendor/phpunit/phpunit/src/Runner/BaseTestRunner.php(65):
PHPUnit\Framework\TestSuite->addTestFiles(Array)
#5 /home/vagrant/code/phirater-l51/vendor/phpunit/phpunit/src/TextUI/Command.php(169):
PHPUnit\Runner\BaseTestRunner->getTest('tes in
/home/vagrant/code/phirater-l51/tests/unit/Phirater/AdditionalCurrencies/CreateAdditionalCurrencyCommandHandlerTest.php
on line 11
My unit tests are in tests/ directory. My UnitTestCase class in extended by \TestCase class and TestCase class is extended by BrowserKitTestCase. What am i doing wrong here? What could be the solution?
If your dependency class is really present and has proper namespace, then the most probable reasons of mentioned error are (avoid copying below examples to your project, that will definetely not work):
composer.json file (root directory) has no relevant record of the dependency (do not forget composer.lock file also), smth like:
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
as a result of above, vendor/autoload.php file (or any of its merged siblings, e.g. vendor/composer/autoload_classmap.php, depending on your case) can miss relevant record of the dependency, smth like:
return array(
'PHPUnit\\Framework\\TestCase' => '/vendor/phpunit/phpunit/src/Framework/TestCase.php',
In most such cases it is really a missed dependency delivered by autoload.php functionality, which in its turn caused by composer malfunctioning. So taking in account all that, try to update composer itself by running composer self-update, and then updating your dependencies by composer update.
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
I was installing slim 3 according to Slim Documentation.After installing i have created index.php file.Given below:
index.php:
<?php
require 'vendor/autoload.php';
$app = new Slim\App();
$app->get('/hello/{name}', function ($request, $response, $args) {
$response->write("Hello, " . $args['name']);
return $response;
});
$app->run();
But when i run the application in http://localhost:8000 then i get a fatal error.I have searched and get some solution in stackoverflow.Those are:
PHP Fatal error: Class 'Slim' not found - Slim Framework 3
PHP Fatal error: Class 'Slim' not found
Composer autoloader + slim framework - fatal error: Class 'Slim\Slim' not found?
But those solution does not solve my problem.Have any specific solution of it?
ERROR:
Fatal error: Class 'Slim\App' not found in
C:\xampp\htdocs\api\index.php on line 5
And my directory format:
UPDATED:
vendor/autoload.php:
<?php
// autoload.php #generated by Composer
require_once __DIR__ . '/composer' . '/autoload_real.php';
return ComposerAutoloaderInit98cfb2e091de2f633f87c81d16402aec::getLoader();
vendor forlder:
The problem is that you have downloaded and extracted Slim manually into htdocs\api\Slim.
It wasn't fetched by Composer, then it would reside in the vendor folder (vendor/slim/slim) and the autoload would work automatically.
Add Slim to your composer.json and run composer install again:
{
"require": {
"slim/slim": "^3.0"
}
}
You already require the Composer Autoloader, so the depenendency should be found and loaded after it was fetched.
Referencing: http://docs.slimframework.com/start/get-started/
i'm trying to use phpunit with zendframework and i'm follwing the tutorial in
https://media.readthedocs.org/pdf/zf2/latest/zf2.pdfhere is my
bootstrap.php
<?php
chdir(dirname(__DIR__));
include __DIR__ . '/../init_autoloader.php';
here is my IndexControllerTest.php
<?php
namespace ApplicationTest\Controller;
use Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase;
class IndexControllerTest extends AbstractHttpControllerTestCase
{
public function setUp()
{
$this->setApplicationConfig(
include '/C:/wamp/www/zf2/config/application.config.php'
);
parent::setUp();
}
public function testIndexActionCanBeAccessed()
{
$this->dispatch('/'); // this is line 20
$this->assertResponseStatusCode(200);
$this->assertModule('application');
$this->assertControllerName('application_index');
$this->assertControllerClass('IndexController');
$this->assertMatchedRouteName('home');
}
}
and i'm getting the follwing errors
Warning: include(C:\wamp\www\zf2\module\Application\test/../init_autoloader.php)
: failed to open stream: No such file or directory in C:\wamp\www\zf2\module\App
lication\test\Bootstrap.php on line 4
Fatal error: Class 'Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase'
not found in C:\wamp\www\zf2\module\Application\test\ApplicationTest\Controller
\IndexControllerTest.php on line 8
i think that's a path probleme (auloading)but i don't know how to fix
any one can help me please ?
Warning: include(C:\wamp\www\zf2\module\Application\test/../init_autoloader.php)
: failed to open stream: No such file or directory in C:\wamp\www\zf2\module\App
lication\test\Bootstrap.php on line 4
This warning is telling you that it can't find the location of your init_autoloader.php file. Assuming that file is located in the root of your ZF2 project (so C:\wamp\www\zf2) as is convention, you need to change:
include __DIR__ . '/../init_autoloader.php';
to
include __DIR__ . '/../../../init_autoloader.php';
EDIT Continued...
PHP Fatal error: Uncaught exception 'RuntimeException' with message 'Unable to
load ZF2. Run php composer.phar install or define a ZF2_PATH environment
variable.' in C:\wamp\www\zf2\init_autoloader.php:48
Your init_autloader.php file is having trouble finding your ZF2 library autoloader. As you're using composer. Add
"zendframework/zendframework": "2.1.*",
to your "require" section in composer.json if its not already there. Run composer and update your vendor libraries with
php composer.phar update
Try run the application again and see if it works. It may do depending what is included in your init_autoload.php file. If you're still having problems add the following to init_autoloader.php
if(file_exists('vendor/autoload.php'))
{
$loader = require 'vendor/autoload.php';
}
Here's the Fix for the fatal error.
You must be missing the 'zend-test' package in your application.
$ composer require zendframework/zend-test